本文小结下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页面了
spring 3 mvc中的controller小结
原创
©著作权归作者所有:来自51CTO博客作者mb5c80f4c73b73a的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:apusic的一个注意的地方
下一篇:mysql中的流程控制语句小结
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
spring mvc中的拦截器小结
在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下。preHandle:预处理回调
spring java 拦截器 mvc