在业务控制方法中写入包装User的模型来收集参数

可以在业务控制方法中书写0个或多个模型来收集客户端的参数

1)  如果多个模型中有相同的属性时,可以用user.name或admin.name来收集客户端参数

2)  用一个新的模型将User和Admin再封装一次

public class User {
    private Integer id;
    private String name;
    private Double sal;
    private Date hiredate;
    public User(){}
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 this.id + ":" + this.name + ":" + this.sal + ":" + this.hiredate;
    }
}
public class Bean {
    private User user;
    private Admin admin;
    public Bean(){}
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Admin getAdmin() {
        return admin;
    }
    public void setAdmin(Admin admin) {
        this.admin = admin;
    }
}
@Controller
@RequestMapping(value = "/person")
public class PersonAction {
    @InitBinder
    protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(
                Date.class, 
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Bean bean,Model model) throws Exception {
        System.out.println(bean.getUser());
        System.out.println(bean.getAdmin());
        System.out.println("PersonAction::add()::POST");
        model.addAttribute("bean",bean);
        return "/register.jsp";
    }
}

  register.jsp

 普通用户
    <form action="${pageContext.request.contextPath}/person/add.action" method="POST">
        编号:<input type="text" name="user.id" value="${bean.user.id}"/><br/>
        姓名:<input type="text" name="user.name" value="${bean.user.name}"/><br/>
        薪水:<input type="text" name="user.sal" value="${bean.user.sal}"/><br/>
        入职时间:<input type="text" name="user.hiredate" value='<fmt:formatDate value="${bean.user.hiredate}" type="both" />'/><br/>
        <input type="submit" value="注册"/>
    </form>

 

posted on 2018-12-04 12:01  LoaderMan  阅读(156)  评论(0编辑  收藏  举报

导航