- public class TableCreatedController extends MultiActionController {
- private TableCreatedService tableCreatedService;
- public TableCreatedService getTableCreatedService() {
- return tableCreatedService;
- }
- public void setTableCreatedService(TableCreatedService tableCreatedService) {
- this.tableCreatedService = tableCreatedService;
- }
- @Override
- protected void bind(HttpServletRequest arg0, Object arg1) throws Exception {
- // TODO Auto-generated method stub
- super.bind(arg0, arg1);
- }
- @Override
- protected ServletRequestDataBinder createBinder(HttpServletRequest request,
- Object command) throws Exception {
- // TODO Auto-generated method stub
- return super.createBinder(request, command);
- }
- @Override
- <SPAN style="COLOR: red"> protected String getCommandName(Object command) {
- // TODO Auto-generated method stub
- return super.getCommandName(command);
- }</SPAN>
- @Override
- protected Method getExceptionHandler(Throwable exception) {
- // TODO Auto-generated method stub
- return super.getExceptionHandler(exception);
- }
- @Override
- public long getLastModified(HttpServletRequest arg0) {
- // TODO Auto-generated method stub
- return super.getLastModified(arg0);
- }
- @Override
- protected ModelAndView handleNoSuchRequestHandlingMethod(
- NoSuchRequestHandlingMethodException ex,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- // TODO Auto-generated method stub
- return super.handleNoSuchRequestHandlingMethod(ex, request, response);
- }
- @Override
- protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
- HttpServletResponse arg1) throws Exception {
- // TODO Auto-generated method stub
- return super.handleRequestInternal(arg0, arg1);
- }
- @Override
- protected void initBinder(HttpServletRequest request,
- ServletRequestDataBinder binder) throws Exception {
- // TODO Auto-generated method stub
- super.initBinder(request, binder);
- }
- @Override
- protected void initBinder(ServletRequest request,
- ServletRequestDataBinder binder) throws Exception {
- // TODO Auto-generated method stub
- super.initBinder(request, binder);
- }
- @Override
- <SPAN style="COLOR: red"> protected Object newCommandObject(Class clazz) throws Exception {
- // TODO Auto-generated method stub
- return super.newCommandObject(clazz);
- }</SPAN>
- }
<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()函数来绑定”。