SpringMVC参数传参:VO参数转换处理
1、参数与VO转换
在使用Spring MVC进行参数接收的时候,不需要在参数前面追加什么特别多的标记,写的基本上都是属性名称。
定义一个Emp程序类,把常用的几种数据类型都加上:
package com.gwolf.vo;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable{
private Integer empno;
private String ename;
private Double sal;
private Date hiredate;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
}

2、那么这个时候如果希望通过SpringMVC自动实现参数的赋值操作。只需要在参数上做一个修改。
package com.gwolf.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.gwolf.vo.Emp;
@Controller
@RequestMapping("/pages/emp/*")
public class EmpAction {
Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("add")
public ModelAndView add(Emp emp) {
ModelAndView md = new ModelAndView("/pages/show.jsp");
System.out.println(emp);
return md;
}
@RequestMapping("remove")
public ModelAndView remove(int id) {
ModelAndView md = new ModelAndView("/pages/show.jsp");
logger.info(id * 2 + "--------");
System.out.println(id * 2 + "--------");
return md;
}
@RequestMapping("list")
public ModelAndView list(@RequestParam(value="cp",defaultValue="1") int currentPage,
@RequestParam(value="ls",defaultValue="10") int lineSize,
@RequestParam(value="col",defaultValue="ename") String colum,
@RequestParam(value="kw",defaultValue="") String keyword) {
ModelAndView md = new ModelAndView();
System.out.println("currentPage=" + currentPage);
System.out.println("lineSize=" + lineSize);
System.out.println("colum=" + colum);
System.out.println("keyword=" + keyword);
return md;
}
@RequestMapping("get")
public ModelAndView doGet(int id,HttpServletRequest request,
HttpServletResponse response) {
ModelAndView md = new ModelAndView();
return md;
}
@RequestMapping("echo")
public ModelAndView echo(String msg) {
ModelAndView md = new ModelAndView("/pages/show.jsp");
md.addObject("info","Echo:" + msg);
return md;
}
}

3、如果现在要进行日期的参数传递,往往需要有一个转换器来控制,这个转换器是一个公共的配置,从一般的开发角度来讲,所有的Action一定要去继承一个父类的Action,这样的好处可以做一些公共的设计。
建立一个AbstractAction程序类,这个程序类里面定义有如下的一段代码。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//本方法的处理指的是添加一个自定义的转换编辑器,如果遇见的操作目标类型为java.util.Date类
//则使用定义好的SimpleDateFormat类来进行格式化处理,并且允许此参数的内容为空
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
}

4、在浏览器中访问地址:
http://localhost:8080/SpringMVC/pages/emp/add.action?empno=10&ename=%E8%80%81%E6%9B%B9&sal=1.1&hiredate=1986-02-12%2020:12:12

5、但是很多时候有可能会出现参数嵌套的情况,例如:一个雇员属于一个部门,可能还会同时传递部门信息。
修改给定的VO类Emp类,同时追加一个Dept类。
package com.gwolf.vo;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable{
private static final long serialVersionUID = -7762107227062576400L;
private Integer empno;
private String ename;
private Double sal;
private Date hiredate;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal + ", hiredate=" + hiredate + "]";
}
}

6、此时对于emp提交的时候可以传递dept的信息,请求路径:
http://localhost:8080/SpringMVC/pages/emp/add.action?empno=10&ename=%E8%80%81%E6%9B%B9&sal=1.1&hiredate=1986-02-12%2020:12:12&dept.deptno=10&dept.dname=%E5%BC%80%E5%8F%91%E9%83%A8
