15SpringMvc_在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

时间:2022-11-12 18:09:01

之前第12篇文章中提到过在业务控制方法中写入普通变量收集参数的方式,也提到了这种凡方式的弊端(参数很多怎么办),所以这篇文章讲的是在业务控制方法中写入模型变量来收集参数。本文的案例实现的功能是,在注册页面(adduser.jsp)上需要输入用户的Id,用户的名字,用户的薪水,用户的入职日期,然后在Action收集,并且在success.jsp中显示出来。

案例如下:

案例结构图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQEAAAFGCAIAAADl071+AAAgAElEQVR4nO2dbXAUx5nHlw/+lg9U4SrnPlzFx/kwDnX2mSDi7DmEs30nfL46u5zCRhUbYyfZGOOYF9uxHFAsAQJFoHidSBhbBkRAeA3CxpFZQzAHGPPmUkDYEBRtBOgFJK12lUWvs1qRvg+zO9M93TM7u9qd6Zl+ftVV7Pb29PSi5z/dvdP/aQ9yFMvvv01+4ZlU5bltu72N0SXk93qSeP2h/B4FTBiP3Q0AAJsBDQCiAxoARAc0AIgOaAAQHdAAIDqgAUB0HKYB5f6ASwj5vTm+GRD02Xh3Ifdfxwp418CR1fce++W/Kul387+Fvz2y+l67GzgxeNdAyO/1eHzBTIpndXbs/qD5s+UK3jVw7LUZoSNbr53Zfu3M9rN7NmxdtgB/e+y1GXY3kDdyqoGQ3+v1etOEZcifrkQ6gj4l9O3oSezUwJEjRyoqKtKUefXb185sj984FD2/69KBt7cuW4C/PfLqt61pqnPIpQZCfq/XHwr6jEM8JxpQ2mzDWM42DRw+fHjGjBnTpk0zLnZo+bevndkePb8ren7XpYObZQ0obw8t19dAOBAoWdwUTtuQoE+7SkfNwXrmoM/j9QeTXbYvqJZi/cWUGrx+f+pvioeK8pqIH9NHYVCDiKBPPlw7sGB+TbUk61vIEqDDEq/Kr/5fef0h9teh/hupk6au/vbMJ+zRgCyABQsWpNXApz+ffnbPhksH3r50cPOputWvPzoXf/vpz6cbHt3ZVF1YU1JYU1LZxi4Q9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yWe5H3FTJcAYrH1ADJjAHuwJ+NbDv/bJwLI6nDP3EWWlAd5dhYw2o78Ec7Dj41cCeneXX++N4ysBPbKwBU8ZixRmcrEIdSYE52F3wq4Fd29d3ReJdkfiUqXMVDZj1E6fRgL6xmJrbYrOFVCSDOdhd8KuBHdsq28NSe1iSL/9y4stPDOZgV8CvBra9V3WlV7rSK+1sOKQIgCs/MZiD3QG/Gqh9903ZQKxJ8qc2+4nBHOwi+NWA7B6mk93tAtwGvxoAAGsADQCiAxoARAc0AIjO/wOvpuFPg8SbGQAAAABJRU5ErkJggg==" alt="" />

上图中的web.xml中内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMvc_10day_self</display-name>
<!-- Spring提供了一个Filter专门用来解决Post提交中文的乱码问题 -->
<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> <servlet>
<!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头, 所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差) -->
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通知DispatcherServlet去指定目录下找到springmvc.xml配置文件 -->
<!--
注意这里的 <param-name>contextConfigLocation</param-name>一个字母都不能有错
一旦有错就会去WEB-INF下面去找
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</web-app>

第二步:写spring.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"
>
<import resource="com/guigu/shen/Action8/springmvc_008.xml"/>
</beans>

第三步:编写springmvc_008.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"
>
<!-- 控制器(程序员)(必须配置) -->
<context:component-scan base-package="com.guigu.shen.Action8"/> <!-- 基于注解的映射器(可选)
这个类和以前的xml方式的类不同,专门是注解用的
-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 基于注解的适配器(可选)
这个类和以前的xml方式的类不同,专门是注解用的
-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 视图解析器(可选)
这个类和以前的xml方式的类一样
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> </beans>

第四步:编写实体类User。

package com.guigu.shen.Action8;

import java.util.Date;

/*
用户
*/
public class User {
private Integer id=1;
private String username;
private Double salary;
private Date hiredate; /**
*
*/
public User() {
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the salary
*/
public Double getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(Double salary) {
this.salary = salary;
}
/**
* @return the hiredate
*/
public Date getHiredate() {
return hiredate;
}
/**
* @param hiredate the hiredate to set
*/
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
} /* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() { return this.id+this.username+this.salary+this.hiredate.toString();
} }

第五步:编写Action8包下面的UserAction.java文件

/**
* Create by 沈晓权
* Create on 2016年8月8日下午8:53:34
*/
package com.guigu.shen.Action8; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
*
请求路径可以拆分为:根模块的名字+分模块的名字
就是相当于当访问http://127.0.0.1:8080:项目名/user/register时就会进入到
registerMethod方法。 */
@Controller
@RequestMapping(value="/user")//根模块的请求名字
public class UserAction {
/*
* 员工注册
*
*/
@RequestMapping(method=RequestMethod.POST,value="/register")//分模块的请求名字
/*
采用模型的方式(User)来收集从页面传来的参数。
方法里面的参数user和以前的参数一样会被用反射的方式去赋值。 */
public String registerMethod(User user,Model model)
{
//打印出user里面的信息
System.out.print("员工的信息是"+user.toString());
//将User绑定到model对象中去。
model.addAttribute("user", user);
//转发到 success.jsp中去。
return "/jsp/success.jsp";
}
@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})//分模块的请求名字
public String loginMethod(Model model,String username)
{
model.addAttribute("message", "员工注册成功");
System.out.println(username);
return "/jsp/success.jsp"; } }

第六步:测试。在adduse.jsp中输入信息。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARsAAAB/CAIAAABQXfGOAAAKg0lEQVR4nO2dTWsbSRrH+9vYyxh08ddw4lwUMPTJX8Bkpse3eI9ugzG280IuOcTJaVloZkfDwjAIJhP2uOwiliAzZNCYxfHKTHYte0A21B76rV67W61Wul7+PwS2SyV1l9Q/P9XV0vN4pBb9fr/f78e/b2xs1HsS3bBvRE5Bv30tvoNevYe5YNSHX0bf//Djn/78F9x0vn3/w48ffhkRGKUh3IhGv158HH9qc4dABT6OP51++DeBURoiGtXm3oDKwChNmdMoz6v5YoI5gVEpke95nudHijs64UD6sEHY8YrurwuMMhQYlRL5nBdpQ6FR8Z1SE+cDRhkKjEqIYw0lRtagNiqNT0rmCFxqo4q3mowARrUFjJIeoqlInXCgMip5lESa7PnmmQkWGkU9ceRT/wbyP2BUW8ComOwsKg1N6XROHnYonQZhh5nzZT7NNxEsMSqSBCo/glHtA6MIIakGeUTy/fysSoxR1CJGKl5ydyPhKR0UPSJFjBqEHXpbMKp9YBQhRAhRnU4njzLFa32URL7fSHSKKTUq/yfA/DuAUS0DowghwtEZq5UcnCVGkTxQNRKcEkpnfT63oSgMBzCqfWAUIWKIYg7WatejmrSJEKz1GQuMIoTw1578SH3cZjO60qXz+eZ/uB5lKDAqJT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AUa5jFVGTafTywLe7ax4Xvc13fS6y7ckrXHju52V7mu64fJ1d2XnXdFGLi8vp9MpjHIW24waF/PTzsrKzsnOCp/YKAk/Oz+Nx+Px+KTrdU+S7t0TumF80k07qYFRLuOOUVIXclWkrTAKzIptRv1Hxdu3j7ue98Xjt0zrq67XfSX0zVrfPv6i+4rp9qrLP4UIjHIZq4y6ubn5uYi/bv/Be/CCbnnxgGv4+cUD+YyQhXsQy83NDYxyFtuMOq3C83WpJstff5fdv/789PT09Luvl9ef0w2nz9ezXkpglMvYZtSwiF6w7K0/o1uerXMNbGsvWF5/xnR7tr4c9Ao3MhzCKJdxyijRH6lRvWA50SY3KvUIRoFirDLq+vr6fQFP73v3n5Y20Y3ffrV8/ynT6+n95a++LdrI+/fvr6+vYZSz2GbUv9Q8uefde1Le9s2XS0tffpP9fu8J0+nJvexOJTDKZawyajKZDJQcrXlrR/FPNWtH0aMlutvSo+hobe1oMIgeLaVd1NuImUwmMMpZ3DHq8wGjXMY2o/6pATDKZawy6urq6h8acHV1BaOcxR6jgiAYjUbj8fjvrTIej0ejEfJMOIs9Ru3t7R0cHJydnV21ytnZ2cHBwR5yIbmKPUYRQnZ3d4MgWGQ2vnKCINjd3a03qHqvBtAKq4wyGhhlBzBKF2CUHRhvVN8imnpZQYsYbxQAWgGjAGgSU416+fLlBgC60qwn1alpFHQCmtOsJ9WpaRQAQAqMAqBJYBQATQKjAGgSGAVAk8AoAJrk/8owb3ZZydl9AAAAAElFTkSuQmCC" alt="" />

但是出现了错误,

错误提示:Field error in object 'user' on field 'hiredate': rejected value [2015-3-2];表示Date类型的2015-3-2不能不能采集到。

那怎么办?参考第八篇博客写日期转换器。第八篇博客里面写的是protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)这个方法。但是我么这里没有,那怎么办?用注解@InitBinder。这样子的话一旦发现Date类型失败,马上去找被@InitBinder注解的方法,然后执行里面的日期转换器。

修改UserAction.java方法

package com.guigu.shen.Action8;

import java.text.SimpleDateFormat;
import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
*
请求路径可以拆分为:根模块的名字+分模块的名字
就是相当于当访问http://127.0.0.1:8080:项目名/user/register时就会进入到
registerMethod方法。
*/
@Controller
@RequestMapping(value="/user")//根模块的请求名字
public class UserAction {
/*
* 员工注册
*
*/
@RequestMapping(method=RequestMethod.POST,value="/register")//分模块的请求名字
/*
采用模型的方式(User)来收集从页面传来的参数。
方法里面的参数user和以前的参数一样会被用反射的方式去赋值。 */
public String registerMethod(User user,Model model)
{
//打印出user里面的信息
System.out.print("员工的信息是"+user.toString());
//将User绑定到model对象中去。
model.addAttribute("user", user);
//转发到 success.jsp中去。
return "/jsp/success.jsp";
}
@RequestMapping(value="/login",method={RequestMethod.POST,RequestMethod.GET})//分模块的请求名字
public String loginMethod(Model model,String username)
{
model.addAttribute("message", "员工注册成功");
System.out.println(username);
return "/jsp/success.jsp"; }
/**
* 自定义类型转换器
* 就是说当Date类型的数据不能得到时,会自动查找@InitBinder注解过的方法
*
*/
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
} }

运行结果:一切正常。