Spring Web MVC 入门实例

来源:互联网 发布:淘宝兼职怎么做单 编辑:程序博客网 时间:2024/04/27 21:02

今天学习了Spring的MVC.根据 http://dev.yesky.com/238/2599738.shtml做了个例子.Spring 的web MVC 框架中担任前端控制器叫色的是org.springframework.web.servlet.DispatcherServlet,DispatcherServlet负责将客户的请求分配给控制对象,所以使用Spring Web MVC的第一步,就是在web.xml中定义DispatcherServlet;

web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>ntx</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/ntx.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>ntx</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
在web.xml中定义了一个DispatcherServlet的实例ntx,从设定中可以看到,所有连接至*.do结尾的请求都会由它来处理,"contextConfigLocation"初始化参数用来设定Bean定义文件的位置与名称,如果不设置,则DispatcherServlet默认会使用Servlet的名称为前置,读取"Servlet 名称_servlet.xml"作为其Bean定义文件,在上面的设定中则读取nxt.xml中的定            义。DispatcherServlet负责分配请求至控制对象(Controller),在Spring Eeb MVC框架中,控制对象要实现org.springframework.web.servlet.mvc.Controller接口,Controller接口有一个必须实现的handleRequest()方法,其定义如下:
package com.wisetop.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import com.wisetop.service.LoginService;public class LoginController implements Controller {private LoginService loginService;private String gotoUrl;public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {String userName = request.getParameter("userName");this.getUserInfo(request, userName);return new ModelAndView(this.getGotoUrl());}private void getUserInfo(HttpServletRequest request, String |userName) {String userInfo = this.getLoginService().getUserInfo (userName);request.setAttribute("userInfo", userInfo);}public String getGotoUrl() {return gotoUrl;}public void setGotoUrl(String gotoUrl) {this.gotoUrl = gotoUrl;}public LoginService getLoginService() {return loginService;}public void setLoginService(LoginService loginService) {this.loginService = loginService;}}其他两个相关的接口和类定义如下:
package com.wisetop.service;public interface LoginService {public String getUserInfo(String userName);}package com.wisetop.serviceimpl;import com.wisetop.service.LoginService;public class LoginServiceImpl implements LoginService {public String getUserInfo(String userName) {return "你的名字是" + userName;}}
Bean文件定义如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans default-autowire="no" default-lazy-init="false"default-dependency-check="none"><bean id="loginService"class="com.wisetop.serviceimpl.LoginServiceImpl" /><bean id="loginController"class="com.wisetop.controller.LoginController"><property name="loginService"><ref bean="loginService" /></property><property name="gotoUrl"><value>/showResult.jsp</value></property></bean><bean id="SimpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandler|Mapping"><property name="mappings"><props><prop key="/userLogin.do">loginController</prop></props></property></bean></beans>
测试页面:
index.jsp<%@ page language="java" pageEncoding="UTF-8"%> <html> <head><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" |content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head>  <body><div><form method="post" action="userLogin.do"><input type="text" name="userName" size="30"/><br/><input type="submit" value="提交 "/><a href="userLogin.do?userName=1">fffff</a></form></div></body></html>
showResult.jsp<%@ page language="java" pageEncoding="UTF-8"%> <html> <head><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" |content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>  <body><% String a=(String)request.getAttribute("userInfo");%><%=a%></body></html>
原创粉丝点击