继承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>
<!-- 视图解析器(省) -->
相关文章推荐
- Spring MVC 之 AbstractCommandController
- 简易的分页方法(鄙人私自集成到了ZF的AbstractActionController.php中)
- Spring MultiActionController 使用 Command 将Form中的数据绑定
- 【旧代码整理】controller继承Action渲染输出html部分
- springmvc入门例子(AbstractCommandController)
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法
- Spring MVC 之 AbstractCommandController
- Spring MVC(四)封装表单参数的控制器(AbstractCommandController)
- MultiActionController绑定command
- 返璞归真asp.net mvc 1.0(3) - Controller/Action
- action,webaction,mode,controller
- WebApi官网学习记录---webapi中controller与action的选择
- Spring MVC MultiActionController example
- MVC中如何在controller的action中输出JS到页面上
- getSource() && getActionCommand
- Neos Flow ActionController 返回JSON
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- Yii里获取当前controller和action的id
- struts2中的action为什么要继承ActionSupport类,不继承也可以,有什么好处?