SpringMVC与Struts的区别。
一、基础jar包
二、①DispatcherServlet,handelMapping,webAction(colltroller),ModelAndView(Jsp,domain),ViewResolver
①配置SpringMVC的分发器Servlet
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
②配置三种常用的处理器映射,映射不同的请求和相应控制器。
1. <!-- beanName处理器映射(http://localhost:8080/SpringMVC/hi.action)(这样的映射可以更加清楚请求的具体控制器的名字) -->
<bean id='beanNameUrlHandlerMapping' class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="1"/>
</bean>
2.<!-- simpleURL处理器映射(http://localhost:8080/SpringMVC/abc.action) (这样可以配置多个链接的名称配置相同控制器来处理)-->
<bean id='simpleUrlHandlerMapping' class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="0"/>
<property name="mappings">
<props>
<prop key="/hi.action">hc</prop>
<prop key="/a.action">hc</prop>
<prop key="/b.action">hc</prop>
<prop key="/c.action">hc</prop>
</props>
</property>
</bean>
3.<!-- ControllerClassName映射(http://localhost:8080/SpringMVC/homeController.action)(直接根据控制器的名字来指定访问的链接,链接为控制器名称的前半部分,就是去掉后面controller后的前半部分,作为链接请求的时候,链接名称需要小写) -->
<bean id="ControllerClassName" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
</bean>
③控制器实现类与三种常用控制器的配置(CommandController,SimpleFormController[post请求],wizardFromController[复杂表单])
1.CommandController
(命令控制器,这个控制器的原理是在控制器中直接注入前端的参数形成的对象;这个过程就是把参数绑定到命令对象中;)
通过命令控制器的构造函数,实现前端参数到控制器中的命令对象绑定;
public class PersonCommandController extends AbstractCommandController { public PersonCommandController(){ //注册命令类 this.setCommandClass(Person.class); this.setCommandName("person"); } @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { Person p = (Person) command; System.out.println(p); return new ModelAndView("index"); } }
2.SimpleFormController[post请求]
(简单表单控制器,如下图所示,当请求到指定的简单表单控制器的时候,根据jsp页面中的get和post方法的不同,经过控制器处理后,get的方法会返回到formView而post方法就会返回到success页面)
public class PersonFormController extends SimpleFormController{ public PersonFormController() { this.setCommandClass(Person.class); this.setCommandName("person"); } @Override protected void doSubmitAction(Object command) throws Exception { Person p = (Person) command; System.out.println(p); } }
3.wizardFormcontroller
(导向标单控制器,控制器中一般有操作取消和保存的响应函数,在级联表单中,取消一般都会返回到第一页,保存就会把页面传递的参数注册到命令对象中)
public class MyWizardController extends AbstractWizardFormController { public MyWizardController() { this.setCommandClass(Person.class); this.setCommandName("person"); } //取消 protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { return new ModelAndView("wizard/jPersonBaseInfo"); } //完成 protected ModelAndView processFinish(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { Person p = (Person) command; System.out.println(p); return new ModelAndView("index"); } }
在控制器中注册Person模型,请求数据被封装到(command对象)。在控制器中操作被转为Person对象的command对象,然后返回实例的ModelAndView,并有配置好的ViewResolver(内部视图资源解析器如下:)
<!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsps/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>