【注】默认按照约定寻找servlet.xml配置文件,如果需要指定或者需要增加多个servlet的配置该如何处理呢?方法如下:
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- <param-value>classpath:/spring-core.xml</param-value> -->
- <!-- <param-value>/WEB-INF/classes/servlet1.xml,/WEB-INF/classes/servlet2.xml</param-value> -->
- <!-- <param-value>/WEB-INF/classes/servlet*.xml</param-value> -->
- <param-value>/WEB-INF/classes/spring/*.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
1、直接转发的controller,不需要再实现无任何行为的controller类,登录常用.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
- <bean id="LoginSpringAction" class="com.alibaba.spring.LoginSpringAction"></bean>
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="start.test">LoginSpringAction</prop>
- <prop key="login.test">LoginAction</prop>
- </props>
- </property>
- </bean>
- <bean id="LoginAction" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
- <property name="viewName" value="login"></property>
- </bean>
- <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView"></property>
- <property name="prefix" value="/WEB-INF/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- </beans>
查找过程:LoginAction->ParameterizableViewController->login->/WEB-INF/jsp/login.jsp
2、请求URL与要找的资源名称一致时,使用更简单的UrlFilenameViewController
- <bean id="LoginAction" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
请求http://localhost:8080/login.test, 通过login.test->LoginAction ->UrlFilenameViewController->解析url,分析出login,再根据规则跳转到login.jsp中
3、封装表单参数的控制器:AbstractCommandController。【注】在spring3.0标注为过期,更建议使用注解。2.5版本未过期。
步骤1:表单提交:<form method="get" action="dispEmp.test">
步骤2:继承AbstractCommandController类
- public class EmployeeController extends AbstractCommandController {
- @Override
- protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
- Object command, BindException errors) throws Exception {
- Employee employee = (Employee)command;
- System.out.println(employee);
- return new ModeAndView("displayEmp");
- }
- }
步骤3:配置CommandController类,【注】需要在里面配置属性:commandClass, 原因是根据反射的原理进行实例化类,再set其属性的
- <bean id="EmployeeAction" class="com.alibaba.spring.EmployeeController">
- <property name="commandClass" value="com.alibaba.spring.Employee"></property>
- </bean>
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="dispEmp.test">EmployeeAction</prop>
- </props>
- </property>
- </bean>
提交到dispEmp.test->EmployeeAction->EmployeeController->commandClass->Employee,执行其set方法将属性赋值。
问题1:Date类型的数据绑定,比如增加了hireDate且格式自定义怎么办呢?方法有2种,其一:自定义属性编辑器注入到AbstractCommandController的方法initBinder中
- @Override
- protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
- throws Exception {
- binder.registerCustomEditor(Date.class, new MyPropertyEditor());
- }
其中:CustomDateEditor代码如下:
- public class MyPropertyEditor extends PropertyEditorSupport {
- String format = "yyyy-MM-dd";
- public void setFormat(String format) {
- this.format = format;
- }
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- SimpleDateFormat dateFormat = new SimpleDateFormat(format);
- System.out.println("--->" + text);
- try {
- Date date = dateFormat.parse(text);
- this.setValue(date);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
其二:使用spring提供的半成品:CustomDateEditor
- @Override
- protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
- throws Exception {
- //binder.registerCustomEditor(Date.class, new MyPropertyEditor());
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(
- "yyyy-MM-dd"), true));
- }
4、另外的获取表单参数的类:SimpleFormController,其onSubmit方法,只有post提交的时候才会执行