Spring MVC入门使用实例详解
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Controller).通过分层使开发的软件结构更清晰,从而达到开发效率的提高,可维护性和扩展性得到提高.Spring提供的MVC框架是在J2EE Web开发中对MVC模式的一个实现,本文通过实例讲解一下Spring MVC 的使用。
相关阅读:
先来看一个HTTP request在Spring的MVC框架是怎么被处理的:(图片来源于Spring in Action)
1,DispatcherServlet是Spring MVC的核心,它的本质是一个实现了J2EE标准中定义的HttpServlet,通过在web.xml配置<servlet-mapping>,来实现对request的监听.
<servlet>
<servlet-name>springTestServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springTestServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
** 以.do结尾的request都会由springTestServlet来处理.
2,3,当接受到request的时候,DispatcherServlet根据HandlerMapping的配置(HandlerMapping的配置文件默认根据<servlet-name>的值来决定,这里会读取springTestServlet-servlet.xml来获得HandlerMapping的配置信息),调用相应的Controller来对request进行业务处理.
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">loginController</prop>
</props>
</property>
</bean>
<bean id="loginController" class="com.test.spring.mvc.contoller.LoginController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value>loginCommand</value>
</property>
<property name="commandClass">
<value>com.test.spring.mvc.commands.LoginCommand</value>
</property>
<property name="authenticationService">
<ref bean="authenticationService"/>
</property>
<property name="formView">
<value>login</value>
</property>
<property name="successView">
<value>loginDetail</value>
</property>
</bean>
** 以login.do结尾的request由loginController来处理.<property name="formView">配置的是Controller接收到HTTP GET请求的时候需要显示的逻辑视图名,本例是显示login.jsp,<property name="successView">配置的是在接收到HTTP POST请求的时候需要显示的逻辑视图名,在本例中即login.jsp提交的时候需要显示名为loginDetail的逻辑视图.
4,Controller进行业务处理之后,返回一个ModelAndView对象.
return new ModelAndView(getSuccessView(),"loginDetail",loginDetail);
5,6,DispatcherServlet根据ViewResolver的配置(本例是在springTestServlet-servlet.xml文件中配置)将逻辑view转换到真正要显示的View,如JSP等.
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
**其作用是将Controller中返回的ModleAndView解析到具体的资源(JSP文件),如上例中的return new ModelAndView(getSuccessView();按照上面ViewResolver配置,会解析成/jsp/loginDetail.jsp.规则为prefix+ModelAndView的第二个参数+suffix.
示例的完整代码如下:
1web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>
prjSpring3</display-name>
<servlet>
<servlet-name>springTestServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springTestServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springTest-services.xml</param-value>
</context-param>
<jsp-config>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
2,springTestServlet-servlet.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">loginController</prop>
</props>
</property>
</bean>
<bean id="loginController" class="com.test.spring.mvc.contoller.LoginController">
<property name="sessionForm">
<value>true</value>
</property>
<property name="commandName">
<value>loginCommand</value>
</property>
<property name="commandClass">
<value>com.test.spring.mvc.commands.LoginCommand</value>
</property>
<property name="authenticationService">
<ref bean="authenticationService"/>
</property>
<property name="formView">
<value>login</value>
</property>
<property name="successView">
<value>loginDetail</value>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>