spring 3 mvc 多个动作分组到一个控制器

本文小结下spring 3 MVC中常见的几个controller相关的

1 MultiActionController
比如在一个controller中,可以设置增删改的操作,都可以放这里:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CustomerController extends MultiActionController{

public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","add() method");

}

public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","delete() method");

}

public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","update() method");

}

public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","list() method");

}

}

<beans ...>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.CustomerController" />

</beans>

下面的形成匹配:
/customer/add.htm –> add()
/customer/delete.htm –> delete()
/customer/update.htm –> update()
/customer/list.htm –> list()
而InternalPathMethodNameResolver是MultiActionController的默认实现,
<beans ...>
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.CustomerController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
<property name="prefix" value="test" />
<property name="suffix" value="Customer" />
</bean>
</property>
</bean>
</beans>
增加了前缀和后缀,比如:
/customer/add.htm –> testaddCustomer()
/customer/delete.htm –> testdeleteCustomer()
/customer/update.htm –> testupdateCustomer()
/customer/list.htm –> testlistCustomer()
如果用annotation则更方便了,上面的变为:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController{

@RequestMapping("/customer/add.htm")
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerAddView");

}
2 PropertiesMethodNameResolver
这个可以配合multication去自定义匹配:
如:
<beans ...>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.CustomerController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/customer/a.htm">add</prop>
<prop key="/customer/b.htm">update</prop>
<prop key="/customer/c.htm">delete</prop>
<prop key="/customer/d.htm">list</prop>
<prop key="/customer/whatever.htm">add</prop>
</props>
</property>
</bean>
</property>
</bean>

</beans>

3 ParameterMethodNameResolver
这个实际上是将新形如XXXX.XXX?ACTION=ADD后的action=add部分映射到
mulitication的又一个方法,很简单,比如:
<beans ...>

<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.CustomerController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"/>
</bean>
</property>
</bean>

</beans>
/customer/*.htm?action=add –> add() method
/customer/whatever.htm?action=add –> add() method
/customer/*.htm?action=update –> update() method
/customer/*.htm?action=delete –> delete() method
/customer/*.htm?action=list –> list() method


4 ParameterizableViewController
一般要经过CONTROLLER才能返回一个VIEW,比如:
public class WelcomeController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView model = new ModelAndView("WelcomePage");
return model;

}

}
但现在实际不用了:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/welcome.htm">welcomeController</prop>
</props>
</property>
</bean>

<bean name="welcomeController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="WelcomePage" />
</bean>

当访问http://localhost/welcome.htm后,直接跳转到WelcomePage.jsp页面了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值