![](http://hi.csdn.net/attachment/201109/8/0_1315452033Lz8q.gif)
控制器是mvc模式中非常重要的部分。
Spring中org.springframework.web.portlet.mvc 包有10种controller :
![](http://www.dlog.cn/uploads/diary/200702/02143944534.jpg)
Spring中org.springframework.web.servlet.mvc包有13种controller :
![](http://www.dlog.cn/uploads/diary/200702/02143957672.jpg)
一 :Controller接口
Spring中最基本的是org.springframework.mvc.Controller接口。
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public interface Controller {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response) throws Exception;
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
}
这个接口非常的简单,只有一个可以返回适当Model和View的请求处理方法。除了这个接口Spring还提供了一些该接口的实现,包括了一些常用的功能方法。
二 :抽象类 AbstractController
AbstractController提供了一个构建Controller的基础。AbstractController继承了WebContentGenerator并实现了Controller接口。这样,在AbstractController中就具备了以下的功能。
ww.dlog
如果你使用AbstractController来做为一个基类的话(不推荐),你需要override一下handleRequestInternal方法,填充你的代码,返回一个ModelAndView对象。例如:
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public class SampleController extends AbstractController {
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response) throws Exception {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
ModelAndView mav = new ModelAndView( " foo " , new HashMap());
return mav;
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
< bean id = " sampleController " class = " samples.SampleController " >
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
< property name = " cacheSeconds " >< value > 120 </ value </ property >
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</ bean >
三 :类 ParameterizableViewController
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public class ParameterizableViewController extends AbstractController {
ParameterizableViewController,根据指定的View名称,返回View。
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
return new ModelAndView( this .viewName);
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
}
四 :类 UrlFilenameViewController
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public class UrlFilenameViewController extends AbstractUrlViewController {
UrlFilenameViewController,从请求URL中得到FileName。
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
String uri = request.getRequestURI();
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
int begin = uri.lastIndexOf( ' / ' );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (begin == - 1 ) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
begin = 0 ;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
begin ++ ;
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
int end;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (uri.indexOf( " ; " ) != - 1 ) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
end = uri.indexOf( " ; " );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else if (uri.indexOf( " ? " ) != - 1 ) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
end = uri.indexOf( " ? " );
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} else {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
end = uri.length();
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
String fileName = uri.substring(begin, end);
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (fileName.indexOf( " . " ) != - 1 ) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
fileName = fileName.substring( 0 , fileName.lastIndexOf( " . " ));
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
return new ModelAndView(fileName);
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
}
五 :类 MultiActionController
这个Controller在一个独立的包中,org.springframework.web.mvc.multiaction。
你可以把多个动作集中到这个Controller中。当你需要大量公用的功能在一个Controller中时,这个Controller是很好的选择。你可以子类化MultiActionController,也可以使用MethodNameResolver。
MethodNameResolver :
当接受请求的时候MultiActionController需要调用一个处理该请求的方法,你可以自己定义个解决方法,通过配置文件中指定的参数名称找到这个方法,方法定义必需使用以下格式:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
ModelAndView actionName(HttpServletRequest, HttpServletResponse);
此外你也可以自己定义一个异常类来处理异常,格式为:
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
ModelAndView anyMeaningfulName(HttpServletRequest, HttpServletResponse, ExceptionClass);
Spring提供了MethodNameResolver的三种方式:
1 : ParameterMethodNameResolver,这个可以根据请求的参数来确定一个需要调用的方法。
例如,http://www.sf.net/index.view?testParam=testIt,这个请求会调用名称为testIt的处理方法。
2 : InternalPathMethodNameResolver,这个可以根据请求的路径名称来调用相应的方法。
例如,http://www.sf.net/testing.view,这个请求会调用testing方法。
3 : PropertiesMethodNameResolver,这个可以根据一个URLs 映射列表来调用相应的方法。
例如,如果定义了/index/welcome.html=doIt,那么当请求为/index/welcome.html时,会调用doIt方法。在定义URLs时可以使用通配符。/**/welcom?.html
例1 :
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<bean id="paramMultiController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="methodNameResolver"><ref bean="paramResolver"/></property>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="delegate"><ref bean="sampleDelegate"/>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</bean>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="paramName"><value>method</value></property>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</bean>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<bean id="sampleDelegate" class="samples.SampleDelegate"/>
SampleDelegate.java 代码
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public class SampleDelegate {
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public ModelAndView retrieveIndex( HttpServletRequest req, HttpServletResponse resp) {
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
rerurn new ModelAndView("index", "date", new Long(System.currentTimeMillis()));
![](https://i-blog.csdnimg.cn/blog_migrate/ecedf933ec37d714bd4c2545da43add2.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f1ba5b45633e9678d1db480c16cae3f.gif)
}
例3 :
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<bean id="paramMultiController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="methodNameResolver"><ref bean="propsResolver"/></property>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="delegate"><ref bean="sampleDelegate"/>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</bean>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<bean id="propsResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<property name="mappings">
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<props>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<prop key="/index/welcome.html">retrieveIndex</prop>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<prop key="/**/notwelcome.html">retrieveIndex</prop>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<prop key="/*/user?.html">retrieveIndex</prop>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</props>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</property>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</bean>
![](http://www.dlog.cn/uploads/diary/200702/02154439390.jpg)
六 :抽象类 BaseCommandController
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public abstract class BaseCommandController extends AbstractController {
这个控制器简化了struts中的操作,可以直接和数据对象进行交互。例如把HttpServletRequest得到的参数动态绑定到dataobject上。
AbstractCommandController,通过这个可以创建你自己的Command Comtroller。它继承了BaseCommandController,可以设置commandObject来保存数据,可以指定验证类。
![](http://www.dlog.cn/uploads/diary/200702/02153532694.jpg)