EmpController.java
package test;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
public class EmpController extends AbstractCommandController{
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
//binder.registerCustomEditor(Date.class, new MyEditor());
//binder.registerCustomEditor(Date.class, new CustomDateEditor(
// new SimpleDateFormat("yyyy-MM-dd"), true));
binder.registerCustomEditor(Date.class, new MyEditor());
//super.initBinder(request, binder);
}
@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
Object command, BindException exception) throws Exception {
Emp emp = (Emp)command;
System.out.println(emp.getEmpNo());
System.out.println(emp.getName() + "---");
System.out.println(emp.getPhone() + "---");
System.out.println(emp.getHireDate());
return null;
}
}
MyEditor.java
package test;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyEditor extends PropertyEditorSupport{
@Override
public String getAsText() {
return super.getAsText();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
// 2012-06-21
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
this.setValue(d);
}
}