将提交参数直接进行实体类的封装
springmvc-test.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!--
可选
映射器(框架) 将bean标签的name属性当作URL请求
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--
可选
适配器(框架) 寻找实现了Controller接口的Action类
也能去找继承了AbstractCommandController类的Action
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 路径前缀 -->
<property name="prefix" value="/jsp/"></property>
<!-- 路径后缀 -->
<property name="suffix" value=".jsp"></property>
<!-- 前缀+视图逻辑名+后缀=真实路径 -->
</bean>
<bean name="/add.action" class="test.EmpAction"></bean>
</beans>
Emp.java
package test;
public class Emp {
private String username;
private String gender;
private String hiredate;
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public Emp() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
EmpAction.java
package test;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
@SuppressWarnings("deprecation")
public class EmpAction extends AbstractCommandController{
public EmpAction() {
//自动将表单参数封装到Emp对象中去
this.setCommandClass(Emp.class);
}
/*
* 自定义类型转换器,将String->Date类型(格式yyyy-MM-dd)
* */
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
/*
* 向springmvc内部注入一个自定义的类型转换器
* 参数一:将String转成什么类型的字节码
* 参数二:自定义转换规则
* true表示该日期字段可以为空
* */
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
/*
* obj表示封装后的实体
* error表示封装时产生的异常
* */
@Override
protected ModelAndView handle(
HttpServletRequest request,
HttpServletResponse response,
Object obj,
BindException error)
throws Exception {
request.setCharacterEncoding("utf-8");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message","增加员工成功");
Emp emp = (Emp)obj;
System.out.println(emp.getUsername()+":"+emp.getGender()+":"+emp.getHiredate());
//将Emp封装到ModelAndView对象中
modelAndView.addObject("emp",emp);
modelAndView.setViewName("success");
return modelAndView;
}
}
success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
success.jsp<br/>
成功<br/>
${requestScope.message}<br/>
姓名:${requestScope.emp.username}<br/>
性别:${requestScope.emp.gender}<br/>
入职时间:${requestScope.emp.hiredate}<br/>
<hr/>
<!--
入职时间:<fmt:formatDate
value="${requestScope.emp.hiredate}"
type="date"
dateStyle="medium"
/>
< % @ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" % >
1)fmt:formatDate 来源于 http://java.sun.com/jsp/jstl/fmt
2)fmt:formatDate作用是格式化日期的显示,例如:2015年5月10日 星期日
3)value表示需要格式化的值
4)type表示显示日期,时间,都显示
type=date表示只显示日期
type=time表示只显示时间
type=both表示日期时间均显示
5)dateStyle表示显示日期的格式:short/medium/default/long/full
-->
</body>
</html>
输出结果
success.jsp
成功
增加员工成功
姓名:123
性别:男
入职时间:2015-5-10