SiteMesh3整合SpringMVC+FreeMarker
SiteMesh3文档 http://wiki.sitemesh.org/wiki/pages/viewpage.action?pageId=1081348
重新搭建项目偶然发现SiteMesh有了新版本SiteMesh3,本着用新不用旧原则果断升级,多少遇了点坑,顺便记录下

SiteMesh3配置
添加maven依赖
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.1</version>
</dependency>添加filter
在web.xml中添加filter
<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>配置servlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>添加sitemesh配置文件
添加配置文件 sitemesh3.xml
默认配置文件路径为:/WEB-INF/sitemesh3.xmlsitemesh3.xml<sitemesh>
<!--
By default, SiteMesh will only intercept responses that set the Content-Type HTTP header to text/html
This can be altered to allow SiteMesh to intercept responses for other types.
默认 SiteMesh 只对HTTP响应头中Content-Type为 text/html 的类型进行拦截和装饰,若需要处理其它mime类型需要自行添加配置
-->
<mime-type>text/html</mime-type>
<!--
Map default decorator. This shall be applied to all paths if no other paths match.
配置装饰器,仅设置decorator参数时表示为默认的装饰器,当没有任何路径被匹配时会使用默认装饰器装配
-->
<mapping decorator="/WEB-INF/decorators/decorator.ftl"/>
<!--对不同的路径指定特定的装饰器-->
<!--<mapping path="/admin/*" decorator="/WEB-INF/decorators/admin-decorator.ftl"/>-->
<!--
Alternative convention. This is more verbose but allows multiple decorators to be applied to a single path.
对同一路径可以同时使用多个装饰器
-->
<mapping>
<path>/category/*</path>
<decorator>/WEB-INF/decorators/common-decorator.ftl</decorator>
<decorator>/WEB-INF/decorators/menu-decorator.ftl</decorator>
<decorator>/WEB-INF/decorators/category-decorator.ftl</decorator>
</mapping>
<!--
Exclude path from decoration.
排除路径,只需将exclue设置为true即可
-->
<mapping path="/static/*" exclue="true"/>
<!--
An advanced feature of SiteMesh is the ability to define custom rules that manipulate tags on a page.
These are classes that implement org.sitemesh.content.tagrules.TagRuleBundle.
默认SiteMesh仅支持title、head、meta、body等tag,可以自定义tag,实现TagRuleBundle接口即可
-->
<content-processor>
<tag-rule-bundle class="com.sankuai.shangchao.util.HtmlTagRuleBundle"/>
</content-processor>
</sitemesh>修改配置文件路径
默认配置文件路径为:/WEB-INF/sitemesh3.xml 若需要修改配置文件路径需要在filter里配置configFile参数<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
<init-param>
<param-name>configFile</param-name>
<param-value>/WEB-INF/sitemesh3.xml</param-value>
</init-param>
</filter>自定义tag
HtmlTagRuleBundle.javaimport org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State; /**
* Description: FootTagRuleBundle
* Author: liuzhao
* Create: 2015-08-22 09:21
*/
public class HtmlTagRuleBundle implements TagRuleBundle {
@Override
public void install(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) {
defaultState.addRule("foot", new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("foot"), false));
} @Override
public void cleanUp(State defaultState, ContentProperty contentProperty, SiteMeshContext siteMeshContext) { }
}
decorator示例
decorator配置页面布局layout,对应的tag会被进行装饰替换
decorator.ftl<!DOCTYPE html><html><head><title><sitemesh:writeproperty="title"/></title><sitemesh:writeproperty='head'/></head><body><h1>啦拉拉,我是卖报的小行家</h1><sitemesh:writeproperty='body'/><sitemesh:writeproperty="foot"/></body></html>SpringMVC、FreeMarker配置(404问题处理)
sitemesh3是完全独立的,不和任何框架进行偶合,因此SpringMVC、FreeMarker配置完全不需要考虑sitemesh3的兼容问题
在加载装饰器的时候,会出现404问题,可能的原因是找不到ftl文件的解析器,所以需要配置下ftl使用默认的Servlet解析,在web.xml中添加如下配置<servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.ftl</url-pattern></servlet-mapping>

decorate源码
@OverrideprotectedbooleanpostProcess(String contentType, CharBuffer buffer,HttpServletRequest request, HttpServletResponse response,ResponseMetaData metaData)throwsIOException, ServletException {WebAppContext context = createContext(contentType, request, response, metaData);Content content = contentProcessor.build(buffer, context);if(content ==null) {returnfalse;}String[] decoratorPaths = decoratorSelector.selectDecoratorPaths(content, context);//遍历装饰器进行装饰for(String decoratorPath : decoratorPaths) {content = context.decorate(decoratorPath, content);}if(content ==null) {returnfalse;}try{content.getData().writeValueTo(response.getWriter());}catch(IllegalStateException ise) {// If getOutputStream() has already been calledcontent.getData().writeValueTo(newPrintStream(response.getOutputStream()));}returntrue;}publicContent decorate(String decoratorName, Content content)throwsIOException {if(decoratorName ==null) {returnnull;}classCharBufferWriterextendsCharArrayWriter {publicCharBuffer toCharBuffer() {returnCharBuffer.wrap(this.buf,0,this.count);}}CharBufferWriter out =newCharBufferWriter();decorate(decoratorName, content, out);CharBuffer decorated = out.toCharBuffer();Content lastContent = currentContent;currentContent = content;try{returncontentProcessor.build(decorated,this);}finally{currentContent = lastContent;}}@Overrideprotectedvoiddecorate(String decoratorPath, Content content, Writer out)throwsIOException {HttpServletRequest filterableRequest =newHttpServletRequestFilterable(request);// Wrap response so output gets buffered.HttpServletResponseBuffer responseBuffer =newHttpServletResponseBuffer(response, metaData,newBasicSelector(newPathMapper<Boolean>(), includeErrorPages) {@OverridepublicbooleanshouldBufferForContentType(String contentType, String mimeType, String encoding) {returntrue;// We know we should buffer.}});responseBuffer.setContentType(response.getContentType());// Trigger buffering.// It's possible that this is reentrant, so we need to take a copy// of additional request attributes so we can restore them afterwards.Object oldContent = request.getAttribute(CONTENT_KEY);Object oldContext = request.getAttribute(CONTEXT_KEY);request.setAttribute(CONTENT_KEY, content);request.setAttribute(CONTEXT_KEY,this);try{// Main dispatch.dispatch(filterableRequest, responseBuffer, decoratorPath);// Write out the buffered output.CharBuffer buffer = responseBuffer.getBuffer();out.append(buffer);}catch(ServletException e) {//noinspection ThrowableInstanceNeverThrownthrow(IOException)newIOException("Could not dispatch to decorator").initCause(e);}finally{// Restore previous state.request.setAttribute(CONTENT_KEY, oldContent);request.setAttribute(CONTEXT_KEY, oldContext);}}protectedvoiddispatch(HttpServletRequest request, HttpServletResponse response, String path)throwsServletException, IOException {//这里调用加载文件会出现404错误 /WEB-INF/decorators/decorator.ftlRequestDispatcher dispatcher = servletContext.getRequestDispatcher(path);if(dispatcher ==null) {thrownewServletException("Not found: "+ path);}dispatcher.forward(request, response);}
SiteMesh3整合SpringMVC+FreeMarker的更多相关文章
- sonne_game网站开发03 spring-mvc+freemarker整合
今天的任务就是在spring+mybatis+springmvc的基础上,将freemarker整合进来. freemarker是什么? freemarker是一种模板引擎.它的目的是基于模板和数据, ...
- spring-mvc+freemarker整合(sonne_game网站开发03)
今天的任务就是在spring+mybatis+springmvc的基础上,将freemarker整合进来. freemarker是什么? freemarker是一种模板引擎.它的目的是基于模板和数据, ...
- SpringBoot整合系列-整合SpringMVC
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9984607.html SpringBoot整合Spring MVC 步骤 第一步:添加必 ...
- freemarker学习 (servlet + freemarker -> Struts2+freemarker -> springMVC+freemarker)
什么是freemarker? freemarker类似于jsp,但不是jsp!怎么说呢?freemarker文件后缀是.ftl,它不像jsp本质是servlet,它将构建模板.解析模板.使用模板分离开 ...
- CXF WebService整合SpringMVC的maven项目
首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html http://blog.csdn.net/hu_shengyang/article/de ...
- SpringMVC+Freemarker+JSTL支持
前提: 网页编程中,我的思路是,通用的模块不仅仅只有后台代码,前端页面也可以独立为模块. 这个和asp.net中的UserController很像 比如有个人员基本信息的展示界面,需要在多个界面中嵌入 ...
- springMVC+freemarker中Could not resolve view with name... 问题解决
用到springMVC+freemarker,自己在做demo过程中报: 严重: Servlet.service() for servlet springmvc threw exception jav ...
- spring整合springmvc和hibernate
上篇文章使用maven搭建了web环境,这篇来记录下如何使用spring整合springmvc和hibernate,亦即spring+springmvc+hibernate框架整合. 第一步:首先配置 ...
- springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置
前面主要是后台代码,spring以及mybatis的整合 下面主要是springmvc用来处理请求转发,展现层的处理 之前所有做到的,完成了后台,业务层和持久层的开发完成了 接下来就是展现层了 有很多 ...
随机推荐
- div+css实例教程
DIV+CSS是WEB设计标准,它是一种网页的布局方法.与传统中通过表格(table)布局定位的方式不同,它可以实现网页页面内容与表现相分离. 对于初学者来说,可能比较模糊不熟悉.毕竟,样式布局需要通 ...
- 设计模型MVC和JavaBean
六.设计模型1和设计模型2(MVC)1.模型1:JSP+JavaBean2.模型2:MVC M:Model模型 JavaBean V:视图 JSP C:控制器 Servlet 七.模型1开发一个简单的 ...
- AngularJS四大特性
Google AnguarJS是一个JS框架,适用于以数据的CRUD操作为主的SPA应用. 四大特性: (1)MVC模型 Model:模型,即数据=>JS中的变量 View:视图,即数据的呈现= ...
- 手机GPS为什么能在室内定位?
为什么手机在室内也能定位?大部分人知道手机会通过GPS进行定位,其实手机定位系统并不是和我们的RTK完全一样的,因为那样就无法解释为何在室内也能定位了,这里我来科普一下智能手机的那些定位方法. ...
- Field 'id' doesn't have a default value(jdbc连接错误)
JDBC 连接错误: 编写数据库连接增添数据时,出现以下错误: error : java.sql.SQLException: Field 'id' doesn't have a default val ...
- innodb的存储结构
如下所示,innodb的存储结构包含:表空间,段,区,页(块),行 innodb存储结构优化的标准是:一个页里面存放的行数越多,其性能越高 表空间:零散页+段 独立表空间存放的是:数据.索引.插入缓冲 ...
- SQL Server 全文索引创建
在安装数据库管理系统SQL Server 后,默认情况下全文索引的服务是没有开启的 ,所以首先需要先开启服务,在sql server配置管理器中 (sql server configuration M ...
- CDATA为何物?
CDATA的解释 1. 术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data),XHTML也是如此. CDATA 部分中的所有内容都会被解析 ...
- EasyUI需注意的问题01
一.EasyUI-Datagrid分页 在创建数据表格(DataGrid)的时候,通过设置'pagination' 属性为 true,可以在数据表格的底部生成一个分页工具栏. <table id ...
- oracle 抛出自定义错误(网上找的例子)
CREATE OR REPALCE TRIGGER minimun_age_checkBEFORE INSERT ON employeeFOR EACH ROWBEGIN IF ADD_MONTHS( ...