您的位置:首页 > 编程语言 > Java开发

继承AbstractCommandController的Action

2017-09-10 22:34 288 查看
@SuppressWarnings("deprecation")
public class EmpAction extends AbstractCommandController{
public EmpAction(){
//将表单参数封装到Emp对象中去
this.setCommandClass(Emp.class);
}
/**
* 自定义类型转换器,将String->Date类型(格式yyyy-MM-dd)
*/
@Override
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
//向springmvc内部注入一个自定义的类型转换器
//参数一:将String转成什么类型的字节码
//参数二:自定义转换规则
//true表示该日期字段可以为空
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
/**
* obj表示封装后的实体
* error表示封装时产生的异常
*/
@Override
protected ModelAndView handle(
HttpServletRequest request,
HttpServletResponse response,
Object obj,
BindException error)throws Exception {

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message","增加员工成功");

Emp emp = (Emp) obj;
System.out.println(emp.getUsername()+":"+emp.getGender()+":"+emp.getHiredate().toLocaleString());

//将Emp封装到ModeAndView对象中
modelAndView.addObject("emp",emp);

modelAndView.setViewName("/jsp/success.jsp");
return modelAndView;
}
}


在springmvc.xml中

<!-- EmpAction处理类 -->
<bean name="/add.action" class="cn.itcast.javaee.springmvc.app09.EmpAction"></bean>
<!-- 映射器(省) -->
<!-- 适配器(省) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器(省) -->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springmvc