Maven下SiteMesh的使用
先白扯两句,为什么用SiteMesh,当然是减少重复代码,让程序员更加转自己的那一小块。
优点呢:结构化,重用
缺点:呵呵呵,性能,尤其是GC
至于3.0是不是有很大提升,请大神来分析一下。
性能你还可以百度一下,哈吼吼。
1. Maven环境项目搭建,省略一千字
2.POM.xml 依赖配置,我这里采用的是3.0.0,
如果是eclipse + tomcat 会自动下载依赖和build,如果修改了配置可能需要手动update和build
详细情况参考:http://mvnrepository.com/artifact/org.sitemesh/sitemesh
<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.0</version>
</dependency>
3. 修改/WEB-INF/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>
具体参考:http://wiki.sitemesh.org/wiki/display/sitemesh3/Getting+Started+with+SiteMesh+3
Creating a decorator
The decorator contains the common layout and style that should be applied to the pages in the web application. It is a template that contains place holders for the content's <title>,<head> and <body> elements.
At the bare minimum, it should contain:
<html>
<head>
<title><sitemesh:write property='title'/></title>
<sitemesh:write property='head'/>
</head>
<body>
<sitemesh:write property='body'/>
</body>
</html>
The <sitemesh:write property='...'/> tag will be rewritten by SiteMesh to include properties extracted from the content. There are more properties that can be extracted from the content and it's possible to define your own rules - that will be covered in another tutorial.
The bare minimum decorator isn't very useful. Let's add some style and a bit of common layout.
Create the file /decorator.html in your web-app, containing:
<html>
<head>
<title>SiteMesh example: <sitemesh:write property='title'/></title>
<style type='text/css'>
/* Some CSS */
body { font-family: arial, sans-serif; background-color: #ffffcc; }
h1, h2, h3, h4 { text-align: center; background-color: #ccffcc;
border-top: 1px solid #66ff66; }
.mainBody { padding: 10px; border: 1px solid #555555; }
.disclaimer { text-align: center; border-top: 1px solid #cccccc;
margin-top: 40px; color: #666666; font-size: smaller; }
</style>
<sitemesh:write property='head'/>
</head>
<body>
<h1 class='title'>SiteMesh example site: <sitemesh:write property='title'/></h1>
<div class='mainBody'>
<sitemesh:write property='body'/>
</div>
<div class='disclaimer'>Site disclaimer. This is an example.</div>
</body>
</html>
In this example, the decorator is a static .html file, but if you want the decorator to be more dynamic, technologies such as JSP, FreeMarker, etc can be used. SiteMesh doesn't care - it just needs a path that can be served content by the Servlet engine.
Configuration
SiteMesh needs to be configured to know about this decorator and what it should do with it.
The configuration file should be created at /WEB-INF/sitemesh3.xml:
<sitemesh> <mapping path="/*" decorator="/decorator.html"/> </sitemesh>
This tells SiteMesh that requests matching the path /* (i.e. all requests) should be decorated with /decorator.html that we just created.
If you don't like the idea of having to use XML to configure SiteMesh, don't worry - there are alternative mechanisms including directly in WEB-INF/web.xml, programatically through a Java API, through Spring, by naming convention, or any custom way you may choose to plug in. These are explained further in another article.
Creating some content
Now to create some content. This is defined in plain HTML content. Create /hello.html:
<html>
<head>
<title>Hello World</title>
<meta name='description' content='A simple page'>
</head>
<body>
<p>Hello <strong>world</strong>!</p>
</body>
</html>
Like the decorator, the content may be static files or dynamically generated by the Servlet engine (e.g. JSP).
The result
Pointing your browser to http://myserver/hello.html will serve the content you just created, with the decorator applied. The resulting merged HTML will look like this:
<html>
<head>
<title>SiteMesh example: Hello World</title>
<style type='text/css'>
/* Some CSS */
body { font-family: arial, sans-serif; background-color: #ffffcc; }
h1, h2, h3, h4 { text-align: center; background-color: #ccffcc;
border-top: 1px solid #66ff66; }
.mainBody { padding: 10px; border: 1px solid #555555; }
.disclaimer { text-align: center; border-top: 1px solid #cccccc;
margin-top: 40px; color: #666666; font-size: smaller; }
</style>
<meta name='description' content='A simple page'>
</head>
<body>
<h1 class='title'>SiteMesh example site: Hello World</h1>
<div class='mainBody'>
<p>Hello <strong>world</strong>!</p>
</div>
<div class='disclaimer'>Site disclaimer. This is an example.</div>
</body>
</html>
As you can see, the <title>, <head> and <body> have been extracted from the content and inserted into the decorator template.
Summary
A quick recap:
- SiteMesh is installed by dropping the library jar in /WEB-INF/lib and creating a filter (with mapping) in /WEB-INF/web.xml
- It can be configured by creating a /WEB-INF/sitemesh3.xml file, or through other configuration methods
- The filter intercepts requests to Content, runs it through the Content Processor and merges with a Decorator
- The Content is defined by an HTML page, that contains the vanilla HTML content of the site
- The Decorator is also defined by an HTML page, that contains the look and feel of the site, and placeholder <sitemesh:write> tags to indicate where the Content should be merged in
- The Content Processor contains the rules for extracting and transforming the content - it has some simple default rules and can be customized
需要注意的
1. 关于 sitemesh3.xm 的配置
path 是网站访路径
decorator 是本地文件路径
Maven下SiteMesh的使用的更多相关文章
- Maven下java.lang.NoClassDefFoundError
本文转载自:http://blog.csdn.net/qqhjqs/article/details/51491516 使用maven管理web项目中jar包之间的依赖,非常的方便好用,但是有时也会出现 ...
- maven下拉项目不能同步
maven下拉项目不能同步,如下图: 解决方法: 对项目右键,team, 选择这里 我选择第一项,有的需要根据情况选择第二项,如果点击完成后,还是不能同步代码,对项目右键,team,disconnec ...
- maven下读取资源文件的问题(转)
原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB ...
- Maven下使用Jetty进行Debug
1.环境和条件 Maven-3.0.3Eclipse请阅读者事先具备一定maven知识 2 配置2.1 添加Jetty插件 在pom.xml中加入如下代码段 <plugin> <gr ...
- maven下配置pom.xml
博主原创,转载请注明. 遇到的问题: 缺少依赖库.解决办法: 在build标签中添加: <plugin> <groupId>org.apache.maven.plugins&l ...
- maven下的sqlserver配置jar包
看了两天的maven,开始把之前做的ssm项目搭建成maven项目,结果在sqlserver的依赖包上受阻,sqlserver需要sqljdbc4.jar包,经过一系列百度教程才得以解决,现在总结一下 ...
- Java-Maven(五):Eclipse&Maven下创建java工程&web工程
本章文章主要学习集成了maven插件的eclipse下,创建java project和web project的步骤. 创建java工程 第一步:使用使用maven project来创建java pr ...
- Maven 下添加oracle11g的包 报Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0
Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0 原因:Oracle 的ojdbc.jar是收费的,所以maven的中央仓库中没有这个资源,只能通过配 ...
- maven下的经常使用的几个元素以及依赖范围的一些知识
maven的pom.xml配置文件里面的project根节点下的dependencies可以包含一个或者多个dependency元素,以声明一个或者多个依赖,每个依赖都可以包含的元素: groupId ...
随机推荐
- Bourbon – 简单轻量的 Sass 混入(Mixins)库
Bourbon 是一个简单易用的 Sass 混入(Mixin)库,无需配置.该混入包含用于支持所有现代浏览器的 CSS3 属性前缀.前缀需要确保在旧的浏览器支持优雅降级.Bourbon 使用 SCSS ...
- Webydo:一款在线自由创建网站的 Web 应用
Webydo 是一款专业的在线建站应用,使平面设计师可以创建和管理 HTML 网站,而无需编写代码.设计人员可以设计任何类型网站,只需要点击按钮,就能够发布先进的 HTML 网站. 你可以控制所有的设 ...
- radialLine 径向线生成器
radialLine 径向线生成器 使用默认的角度,半径访问器,和插值器函数构造一个 径向线生成器,返回的函数用以生成开放分段线性曲线.折线.它和line生成器很相似,只是将line的x,y访问其换位 ...
- javascript学习笔记(四):事件处理函数和动态创建html标记。
1 HTML的事件属性 全局事件属性:HTML 4 增加了使事件在浏览器中触发动作的能力,比如当用户点击元素时启动 JavaScript. a. Window 事件属性,针对 window 对象触发 ...
- Django之Form、CSRF、cookie和session
Django是一个大而全的web框架,为我们提供了很多实用的功能,本文主要介绍Form.CSRF.cookie和session 一.Form 在web页面中form表单是重要的组成部分,为了数据安全和 ...
- elasticsearch 集群
elasticsearch 集群 搭建elasticsearch的集群 现在假设我们有3台es机器,想要把他们搭建成为一个集群 基本配置 每个节点都要进行这样的配置: cluster.name: ba ...
- Java魔法堂:URI、URL(含URL Protocol Handler)和URN
一.前言 过去一直搞不清什么是URI什么是URL,现在是时候好好弄清楚它们了!本文作为学习笔记,以便日后查询,若有纰漏请大家指正! 二.从URI说起 1. 概念 URI(Uniform Reso ...
- 在CentOS上部署Asp.Net MVC3的第一次尝试
关注mono已经很久了,现在才有时间真正的尝试一下在linux中部署asp.net的网站,也算是记录 一下自己的第一次尝试吧. 我的实践的环境是win7 + VM10 + CentOS6.5 下面就是 ...
- ASP.NET MVC Model绑定小结
Model绑定是指从URL提取数据,生成对应Action方法的参数这个过程.前面介绍的一系列Descriptor负责提供了控制器,行为方法和参数的元数据,ValueProvieder负责获取数据,剩下 ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(十)DOSProtection
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...