精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-01-17
最后修改:2012-01-18
之前改写的spring web flow发行包中的booking-mvc应用(参见http://clongjava.iteye.com/blog/1314108),采用的是tiles2来统一整个站点页面风格,spring web flow提供了一个ViewResolver为org.springframework.js.ajax.AjaxUrlBasedViewResolver,它结合了spring-js,提供了ajax内置的支持,可以使我们局部加载某个tiles的put-attribute中的内容。可是每次都要写tiles配置文件,感觉不爽,由于之前使用过sitemesh感觉不错,就想替换掉tiles,由于tiles和sitemesh采用统一页面风格的理念不一样,sitemesh采用的是装饰(decorator)通过filter来过滤所有请求生成的页面。而tiles是采用的模板方法。二者目的都是一样的,统一风格 。 sitemesh配置很简单:首先在web.xml中添加一个sitemesh filter,代码如下: <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <!-- note:sitemesh filter mapping must appear after springSecurityFilterChain filter mapping --> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 这里有个注意点要提下:当我们也配置了spring security filter的时候,sitemesh的filter mapping应该在spring security filter mapping之后,不然sitemesh对于使用spring security jsp tag的页面显示不出来,这也可以理解,sitemesh已经装饰完成了,而spring security filter还没结束,当然显示不出来。
第二步,在WEB-INF目录下,新建一个名为decorators.xml文件即可,内容如下: <?xml version="1.0" encoding="UTF-8" ?> <decorators defaultDir="/WEB-INF/layout"> <excludes> <pattern>/resources/*</pattern> </excludes> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator> </decorators> 第三步,开始编写我们的装饰者页面,这里展示一下我的页面: <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><decorator:title default="Spring Travel: Spring MVC and Web Flow Reference Application" /></title> <link type="text/css" rel="stylesheet" href="<c:url value="/resources/dijit/themes/tundra/tundra.css" />" /> <link rel="stylesheet" type="text/css" href="<s:url value="/resources/css/main.css" />" /> <!--[if lte IE 6]> <link rel="stylesheet" type="text/css" href="<s:url value="/resources/css/ie6.css" />" /> <![endif]--> <script type="text/javascript" src="<c:url value="/resources/dojo/dojo.js" />"></script> <script type="text/javascript" src="<c:url value="/resources/spring/Spring.js" />"></script> <script type="text/javascript" src="<c:url value="/resources/spring/Spring-Dojo.js" />"></script> <script type="text/javascript" src="<s:url value="/resources/js/jquery-1.7.1.min.js" />"></script> <script type="text/javascript" src="<s:url value="/resources/js/jquery.validate.min.js" />"></script> <decorator:head /> </head> <body class="tundra"> <div id="wrapper"> <div id="header"> <div id="topbar"> <p> <sec:authorize access="hasRole('ROLE_USER')"> <c:if test="${pageContext.request.userPrincipal != null}"> Welcome, ${pageContext.request.userPrincipal.name} | </c:if> <a href="<c:url value="/logout" />">Logout</a> | <a href="<c:url value="/sec/users/changePassword" />">Change Password</a> </sec:authorize> <sec:authorize access="anonymous"> <a href="<c:url value="/login" />">Login</a> </sec:authorize> </p> </div> <div id="logo"> <p> <a href="<s:url value="/" />"><img src="<s:url value="/resources/images/header.jpg" />" alt="generic hotel" /></a> </p> </div> </div> <div id="content"> <div id="primary"> <decorator:body /> </div> <div id="secondary"> <p> <a href="http://www.thespringexperience.com"> <img src="<s:url value="/resources/images/myDog.jpg" />" width="208px" height="300px" alt="generic hotel" /> </a> </p> <p class="center"> <a href="http://www.thespringexperience.com"> <img src="<s:url value="/resources/images/springone2gx.jpeg" />" alt="SpringOne 2GX" /> </a> </p> </div> </div> <div id="footer"> <a href="http://www.springframework.org"> <img src="<s:url value="/resources/images/powered-by-spring.png" />" alt="Powered by Spring" /> </a> </div> </div> </body> </html> 这里充分显示你的前端水平,规划好整个风格后,后面开发起来就轻松多了。 传几张图片看看效果图吧,图中的dog是我养的,非常棒。
本来打算使用sitemesh3的,它里面有些新的东西,对返回的MIME不是text/html的响应也能进行装饰,可是发现sitemesh3有一个问题,当第一次访问页面的时候没有问题,可是一刷新页面就会出现页面加载缓慢,并抛出一个异常,异常如下: [DEBUG,FlowHandlerMapping,qtp1056001-20 - /SpringWebFlowTutorial/resources/js/jquery.validate.min.js] No flow mapping found for request with URI '/SpringWebFlowTutorial/resources/js/jquery.validate.min.js' [DEBUG,RequestMappingHandlerMapping,qtp1056001-20 - /SpringWebFlowTutorial/resources/js/jquery.validate.min.js] Looking up handler method for path /resources/js/jquery.validate.min.js [DEBUG,DispatcherServlet,qtp1056001-24 - /SpringWebFlowTutorial/resources/js/jquery-1.7.1.min.js] Could not complete request java.lang.IllegalStateException: NO CONTENT at org.eclipse.jetty.http.HttpGenerator.addContent(HttpGenerator.java:156) at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:167) at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:101) at org.sitemesh.webapp.contentfilter.io.RoutableServletOutputStream.write(RoutableServletOutputStream.java:133) at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:113) at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.writeContent(ResourceHttpRequestHandler.java:240) at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:141) at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:705) at javax.servlet.http.HttpServlet.service(HttpServlet.java:814) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:547) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1359) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1330) at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169) at org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126) at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:163) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1330) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1330) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:478) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:941) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:409) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:875) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110) at org.eclipse.jetty.server.Server.handle(Server.java:345) at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:441) at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:919) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533) at java.lang.Thread.run(Thread.java:662) 这个只有在刷新当前页面的时候才会出现,我想就是在加载静态资源文件时会出错,可能是sitemesh3的一个bug,毕竟sitemesh3还是alpha版本,不知道有没有高人遇到过这个问题,帮小弟解答一下。
sitemesh3配置更简单,只需在WEB-INF下放置sitemesh3.xml即可。 <?xml version="1.0" encoding="UTF-8"?> <sitemesh> <!-- Map default decorator. This shall be applied to all paths if no other paths match. --> <mapping path="/*" decorator="/WEB-INF/layout/main.jsp" exclue="false"/> <!-- Exclude path from decoration. --> <mapping path="/resources/*" exclue="true"/> </sitemesh> web.xml过滤器配置如下: <filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 当然了应该把sitemesh3的maven依赖加进去。pom.xml <dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0-alpha-2</version> </dependency> 对于使用maven作为项目管理工具的,应该拥有自己的一台私服,比如nexus,这样构件搜索起来比较快速。
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-01-17
不错 连续看了你写的这两篇文章,收获颇丰,谢谢你~
|
|
返回顶楼 | |
发表时间:2012-01-17
liubey 写道 不错 连续看了你写的这两篇文章,收获颇丰,谢谢你~
大家一起学习进步啊,官方文档里都有详细的教程,我只不过自己改写了一下,细节的东西还是看文档,很详细。一直比较闲,项目考虑到我们是新人,不给我们上啊,一帮所谓的“有经验的人”上了,我看有的还不如我们新人,受不了,没发言权,哎 |
|
返回顶楼 | |
浏览 4352 次