<!-- 编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 映射器(框架)
BeanNameUrlHandleMapping表示benan标签
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 适配器(框架) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器(框架) 通过 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 路径前缀 -->
<property name="prefix" value="/jsp/"/>
<!-- 路径后缀 -->
<property name="suffix" value=".jsp"/>
<!-- 前缀+视图逻辑名+后缀=真实路径 -->
</bean>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
/**
* 单例
* 处理类
* @author AdminTC
*/
@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 {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message","增加员工成功");
Emp emp = (Emp) obj;
System.out.println(emp.getUsername()+":"+emp.getGender()+":"+emp.getHiredate().toLocaleString());
//将Emp封装到ModeAndView对象中
modelAndView.addObject("emp",emp);
modelAndView.setViewName("success");
return modelAndView;
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>演示二个控制器</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/index.action" style="text-decoration:none">首页</a>
<hr/>
<form action="${pageContext.request.contextPath}/add.action" method="POST">
<table border="2" align="center">
<tr>
<th>姓名</th>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<th>性别</th>
<td>
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女" checked/>女
</td>
</tr>
<tr>
<th>入职时间</th>
<td>
<input type="text" name="hiredate" value="2018-4-22"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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"
/>
</body>
</html>
<!--
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
-->
原文地址:http://blog.51cto.com/357712148/2106374