javaBean
package com.deng.domain; import java.util.Date; public class User { private int id; private String username; private String password; private String email; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
register.jsp
<form action="${pageContext.request.contextPath }/servlet/regServlet" method="post"> 用户名:<input type="text" name="username" value="${uf.username }"/>${uf.msg.username }${error }<br/> 密码:<input type="password" name="password" />${uf.msg.password }<br/> 确认密码:<input type="password" name="repassword"/>${uf.msg.repassword }<br/> 油箱:<input type="text" name="email" value="${uf.email }"/>${uf.msg.email }<br/> 生日:<input type="text" name="birthday" value="${uf.birthday }"/>${uf.msg.birthday }<br/> <input type="submit" value="注册"/><br/>
regservlet
User u = new User(); try { BeanUtils.populate(u, request.getParameterMap()); } catch (Exception e1) { e1.printStackTrace(); } User user = new User(); try { /* * ConvertUtils.register(new Converter() {//注册一个日期转换器 * * public Object convert(Class type, Object value) { Date date1 = * null; if(value instanceof String){ String date = (String) value; * SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { * date1 = sdf.parse(date); } catch (ParseException e) { * e.printStackTrace(); } } return date1; } }, Date.class); */ ConvertUtils.register(new DateLocaleConverter(), Date.class);
//处理时间格式
DateConverter dateConverter = new DateConverter();
//设置日期格式
dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"});
//注册格式
ConvertUtils.register(dateConverter, Date.class);
//封装数据
BeanUtils.populate(bean, properties);