java spring mvc 自省机制填充

jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
//JSP页面
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>录入页面</title>
</head>
<body>
	<div id="main">
		<form action="user.htm" id="userForm" method="POST">
			<span class="title">姓名:</span>
			<span><input name="name" value="${user.name}" /></span><br />
			<span class="title">生日:</span>
			<span><input name="birthday" 
					value="<fmt:formatDate value='${user.birthday}' pattern='yyyy-MM-dd'/>" />
				(日期格式:yyyy-MM-dd)
			</span><br />
			<span><input type="submit" value="提交" /></span>
			<span>
				<input type="hidden"" name="op" value="${op}" />
				<input type="hidden"" name="id" value="${user.id}" />
			</span>
		</form>
	</div>
</body>
</html>

//User.java实体类

package spr.domain;

import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class User implements Serializable {
	
	private Integer id;
	private String name;
	private Date birthday;
	
	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 Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}

控制类(action controller)

package spr.web;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import spr.domain.*;
import spr.service.*;

@Controller
@RequestMapping("/user.htm")
public class UserController {
	@Autowired
	private UserServiceAble userService;
	
	//日期类型映射器
	@InitBinder
	public void initBinder(WebDataBinder binder){
		SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
	}
	
	@RequestMapping(params="op=doAdd")
	public String doAdd(User user){
		System.out.println(user.getName());
		userService.addUser(user);
		return "redirect:user.htm?op=list";
	}

}

  

posted @ 2013-10-25 15:16  暖流  阅读(516)  评论(0编辑  收藏  举报