StevenBot-Saltsam

眼睛能装下世界,为何却装不下眼泪? 一只风筝一辈子只为一根线冒险。 那不是一场游戏,为何总有一根线牵着心怀,隐隐作疼? 那不是一段邂逅,为何飘在桥上的影子,总缠进梦乡? 那不是一个梦境,为何你的温柔私语,总是不经意的响起?

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

datas link

OSChinal Sources codes Library

搜索

最新评论

阅读排行榜

评论排行榜

SpringMVC web框架学习 Controller 分析

SpringMVC web框架学习 Controller 分析

org.springframework.web.servlet.mvc.AbstractController (implements org.springframework.web.servlet.mvc.Controller)

Spring MVC框架中的Controller对请求进行处理:所有的Controller都实现接口Controller:

public interface Controller {       /**      * Process the request and return a ModelAndView object which the DispatcherServlet      * will render. A <code>null</code> return value is not an error: It indicates that      * this object completed request processing itself, thus there is no ModelAndView      * to render.      * @param request current HTTP request      * @param response current HTTP response      * @return a ModelAndView to render, or <code>null</code> if handled directly      * @throws Exception in case of errors      */     ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;   } 上面的doc表明Controller返回的modelandview可以使空,表明请求都是该函数中处理完成了,不需要modeland来进行渲染。
 
在继续之前先介绍一个有用的工具类:WebUtils。用这个可以简化session,request的处理。具体的内容可以参考文档。
 
Controller的第一个实现是:AbstractController。他是一个Abstract类,除了实现了Controller接口,它还继承了WebContentGenerator。
 
WebContentGenerator的作用是什么?参考文档可以发现,该类主要对Cache和Session进行管理。
 
cacheSeconds 指定内容缓存的时间,默认为1
requireSession 是否需要会话,默认支持
supportedMethods 支持的方法,默认是GET\post\Head
useCacheControlHeader 指定是否使用http1.1的cache控制头信息,默认使用
useCacheControlNoStore 指定是否设置http1.1的cache控制头信息为no-store。默认使用
useExpiresHeader 指定是否使用http1.0的expire头信息。默认使用
用户可以对这些参数进行测试,cache和expire信息涉及到了http协议信息,更多信息可以参考http协议文档。这里不再说明。
 
再看AbstractController的代码:
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {

// Delegate to WebContentGenerator for checking and preparing.
checkAndPrepare(request, response, this instanceof LastModified);

// Execute handleRequestInternal in synchronized block if required.
if (this.synchronizeOnSession) {
HttpSession session = request.getSession(false);
if (session != null) {
Object mutex = WebUtils.getSessionMutex(session);
synchronized (mutex) {
return handleRequestInternal(request, response);
}
}
}
return handleRequestInternal(request, response);
}
checkandPrepare的目的就是使用用于进行的配置来对request进行预处理和准备。
他会检查支持的方法,和会话,然后应用cache设置。
如果需要session同步,就进行同步处理。session同步应用于有session的情况下。如果没有session,session同步是没有用的。
AbstractController会调用handleRequestInternal方法进行处理,继承AbstractController的类需要实现该方法。
下面我们再看看AbstractUrlViewController 的代码实现和文档,先看handleRequestInternal的实现:

 
/**
* Retrieves the URL path to use for lookup and delegates to
* {@link #getViewNameForRequest}.
*/
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
String viewName = getViewNameForRequest(request);
if (logger.isDebugEnabled()) {
logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
}
return new ModelAndView(viewName);
}
可以看到,它使用了getViewNameForRequest获取需要的viewName。而getViewNameForRequest是一个抽象函数,需要子类实现。lookupPath就是我们请求的URL中的一部分。如我们使用UrlFilenameViewController来进行如下的配置:
<bean name="/index.do" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>、
09-11-25 11:56:06 - DEBUG [http-8200-1] - Returning view name 'index' for lookup path [/index.do]
该Controller对/index.do解析成index,然后再通过viewResolver对index进行扩展为/jsp/index.jsp。从而找到该页面。
可以看到这个类的主要是用于对url进行解析,然后转到合适的页面上,而在转到这个页面之前不需要进行特别的处理。
明白了该类的作用自然也就知道了UrlFilenameViewController的作用。这里不再进行详细分析。

posted on 2011-10-15 09:49 Steven_bot 阅读(700) 评论(0)  编辑  收藏 所属分类: spring


只有注册用户登录后才能发表评论。


网站导航: