Spring中MVC模块代码详解
springmvc的controller用于处理用户的请求。controller相当于struts1里的action,他们的实现机制、运行原理都类似
controller是个接口,一般直接继承abstrcatcontroller,并实现handlerequestinternal方法。handlerequestinternal方法相当于struts1的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); return new modelandview("cat/listcat"); } }
springmvc没有内置数据的封装,开发者可以自己封装数据转换代码
springmvc独特的地方在于view层的处理上。handlerequestinternal返回modelandview对象,可以看做是对jsp对象的封装。modelandiview直接接受jsp页面的路径。例如参数"cat/listcat",只是jsp路径的一部分,实际完整的路径是"web-inf/jsp/cat/catlist.jsp",路径前后的部分是配置在配置文件中的
除了制定jsp路径,modelandview还可以直接传递model对象到view层,而不用事先放到request中,例如newmodelandview("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"?> <!dctypebeans public "-//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>
总结
以上就是本文关于spring中mvc模块代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。