MultiActionController 的方法

springmvc的多action用法,我在用的时候起初想把crud都放到里面,可是实现的时候不知道该怎么绑定command对象,
Java代码 复制代码
  1. public class TableCreatedController extends MultiActionController {   
  2.     private TableCreatedService tableCreatedService;   
  3.   
  4.     public TableCreatedService getTableCreatedService() {   
  5.         return tableCreatedService;   
  6.     }   
  7.   
  8.     public void setTableCreatedService(TableCreatedService tableCreatedService) {   
  9.         this.tableCreatedService = tableCreatedService;   
  10.     }   
  11.   
  12.     @Override  
  13.     protected void bind(HttpServletRequest arg0, Object arg1) throws Exception {   
  14.         // TODO Auto-generated method stub   
  15.         super.bind(arg0, arg1);   
  16.     }   
  17.   
  18.     @Override  
  19.     protected ServletRequestDataBinder createBinder(HttpServletRequest request,   
  20.             Object command) throws Exception {   
  21.         // TODO Auto-generated method stub   
  22.         return super.createBinder(request, command);   
  23.     }   
  24.   
  25.     @Override  
  26. <SPAN style="COLOR: red">   protected String getCommandName(Object command) {   
  27.         // TODO Auto-generated method stub   
  28.         return super.getCommandName(command);   
  29.     }</SPAN>   
  30.     @Override  
  31.     protected Method getExceptionHandler(Throwable exception) {   
  32.         // TODO Auto-generated method stub   
  33.         return super.getExceptionHandler(exception);   
  34.     }   
  35.   
  36.     @Override  
  37.     public long getLastModified(HttpServletRequest arg0) {   
  38.         // TODO Auto-generated method stub   
  39.         return super.getLastModified(arg0);   
  40.     }   
  41.   
  42.     @Override  
  43.     protected ModelAndView handleNoSuchRequestHandlingMethod(   
  44.             NoSuchRequestHandlingMethodException ex,   
  45.             HttpServletRequest request, HttpServletResponse response)   
  46.             throws Exception {   
  47.         // TODO Auto-generated method stub   
  48.         return super.handleNoSuchRequestHandlingMethod(ex, request, response);   
  49.     }   
  50.   
  51.     @Override  
  52.     protected ModelAndView handleRequestInternal(HttpServletRequest arg0,   
  53.             HttpServletResponse arg1) throws Exception {   
  54.         // TODO Auto-generated method stub   
  55.         return super.handleRequestInternal(arg0, arg1);   
  56.     }   
  57.   
  58.     @Override  
  59.     protected void initBinder(HttpServletRequest request,   
  60.             ServletRequestDataBinder binder) throws Exception {   
  61.         // TODO Auto-generated method stub   
  62.         super.initBinder(request, binder);   
  63.     }   
  64.   
  65.     @Override  
  66.     protected void initBinder(ServletRequest request,   
  67.             ServletRequestDataBinder binder) throws Exception {   
  68.         // TODO Auto-generated method stub   
  69.         super.initBinder(request, binder);   
  70.     }   
  71.   
  72.     @Override  
  73. <SPAN style="COLOR: red">   protected Object newCommandObject(Class clazz) throws Exception {   
  74.         // TODO Auto-generated method stub   
  75.         return super.newCommandObject(clazz);   
  76.     }</SPAN>   
  77.   
  78.   
  79.   
  80. }  

<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName"><value>method</value>
</property>
<property name="defaultMethodName"><value>view</value>
</property>
</bean>

<bean name="/jsp/test.do" class="org.nightwalker.spaces.web.controller.TestController">
<property name="methodNameResolver">
<ref local="methodNameResolver"/>
</property>
</bean>

methodNameResolver指定了调用MultiActionController中方法的方式,例如,如果在你的MultiActionController中有如下方法:
public ModelAndView insertAccount(HttpServletRequest request,HttpServletResponse,Account account)
{
}

那么要调用这个方法,你的JSP页面的请求URL就应该这样写:
<form action="test.do?method=insertAccount">
</form>

至于绑定什么command和JSP是没有关系的。你在MultiActionController中的方法签名第三个参数是什么对象,Spring就会把request中参数的值绑定到方法签名的第三个对象中。

奥妙就在MultiActionController的invokeNamedMethod方法里

protected final ModelAndView invokeNamedMethod(String methodName, HttpServletRequest request, HttpServletResponse response) throws Exception
{



// If last parameter isn't of HttpSession type, it's a command.
// 在这里,Spring会判断你的请求方法里面第三个参数的类型。如果第三个参数类型不是HttpSession,就认为是要绑定的command。
if (method.getParameterTypes().length >= 3 &&
!method.getParameterTypes()[method.getParameterTypes().length - 1].equals(HttpSession.class))
{
// 根据类型利用反射产生Command对象
Object command = newCommandObject(method.getParameterTypes()[method.getParameterTypes().length - 1]);
params.add(command);
// 绑定command对象
bind(request, command);
}


// 最后通过反射去调用你的方法,也就是说,在执行你的方法时,command已
// 经帮你绑定好拉,你直接用就可以了。
return (ModelAndView) method.invoke(this.delegate, params.toArray(new Object[params.size()]));
}
不过在他的源码里有说明 -“--靠第3个参数来绑定比较缺乏灵活性,还是建议你调用它的bind()函数来绑定”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值