Spring之MVC模块
Spring MVC的Controller用于处理用户的请求。Controller相当于Struts 1里的Action,他们的实现机制、运行原理都类似
Controller是个接口,一般直接继承AbstrcatController,并实现handleRequestInternal方法。handleRequestInternal方法相当于Struts 1的execute方法
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class CatController extends AbstractController{ private ICatService catService; //setter、getter略 protected ModelAndView handleRequestInternal(HttpServletRequestrequest,HttpServletResponse response) throws Exception{ String action =request.getParameter("action"); if("list".equals(action)){ return this.list(request,response); } } protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{ List<Cat> catList = catService.listCat(); request.setAttribute("catList", catList); returnnew ModelAndView("cat/listCat"); } }
Spring MVC没有内置数据的封装,开发者可以自己封装数据转换代码
Spring MVC独特的地方在于view层的处理上。handleRequestInternal返回ModelAndView对象,可以看做是对JSP对象的封装。ModelAndIView直接接受JSP页面的路径。例如参数"cat/listCat",只是JSP路径的一部分,实际完整的路径是"WEB-INF/jsp/cat/catList.jsp",路径前后的部分是配置在配置文件中的
除了制定JSP路径,ModelAndView还可以直接传递Model对象到View层,而不用事先放到request中,例如new ModelAndView("cat/listCat","cat",cat),如果传递多个参数,可以使用Map,如
Map map = newHashMap(); map.put("cat",cat); map.put("catList",catList); return new ModelAndView("cat/listCat",map);
一般使用一个独立的xml文件如spring-action.xml专门配置web相关的组件
<?xml version= "1.0" encoding="UTF-8"?> <!DCTYPE beansPUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value><!-- JSP前缀--> </property> <property name="suffix"> <value>.jsp</value> <!-- JSP后缀--> </property> <!-- 配置URL Mapping--> <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping"> <property name="mappings"> <props><!—Controller的URL会被配置成"cat.mvc"--> <prop key="cat.mvc">catController</prop> <props> </property> </bean> <bean id="catController" class="com.clf.spring.CatController"> <property name="catService" ref="catService"></property> </bean> </beans>
web.xml配置
<context-param><!-- Spring配置文件的位置--> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml, /WEB-INF/classes/spring-action.xml </param-value> </context-param> <listener><!-- 使用Listener加载Spring配置文件--> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet><!-- spring分发器--> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring-action.xml</param-value> </init-param> <load-on-startup>1</load-on-startup><!-- 启动时加载--> </servlet> <servlet-mapping> <servlet-name> spring </servlet-name> <url>*.mvc</url> </servlet-mapping>
如果一个Controller要处理多个业务逻辑,可以使用MultiActionController,相当于Struts 1中的DispatchAction,能根据某个参数将不同的请求分发到不同的方法上
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class CatController extends AbstractController{ private ICatService catService; //setter、getter略 protected ModelAndView add(HttpServletRequestrequest,HttpServletResponse response) throws Exception{ …… return new ModelAndView("cat/addCat"); } protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{ List<Cat> catList = catService.listCat(); request.setAttribute("catList", catList); return new ModelAndView("cat/listCat"); } }
配置到spring-action.xml
<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName"> <value>action</value><!-- 配置分发参数--> </property> <property name="defaultMethodName"> <value>list</value><!-- 配置默认的执行方法--> </property> </bean> <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping"> <property name="mappings"> <props> <prop key="cat.mvc">catController</prop><!-- 访问"cat.mvc"则交给catController处理--> <prop key="catMulti.mvc">catMultiController</prop><!-- 访问"catMulti.mvc"则交给catMultiController处理--> <props> </property> </bean> <bean id="catController" class="com.clf.spring.CatMultiController"> <property name="catService" ref="catService"></property> </bean> <bean id="catMultiController" class="com.clf.spring.CatController"> <property name="catService" ref="catService"></property> </bean>