mesh-site 例子
2012-09-02 00:00
399 查看
例子1
在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator<decorator name="mydecorator1" page="mydecorator1.jsp"> <pattern>/test1.jsp</pattern> </decorator> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <html> <head> <title>My Site - <decorator:title default="Welcome!" /></title> <decorator:head /> </head> <body> <decorator:body /> <p>This message is in /decorators/mydecorator1.jsp</p> </body> </html> |
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>This is test1</title> </head> <body> <b>This is test1</b> </body> </html> |
This is test1 This message is in /decorators/mydecorator1.jsp |
例子2 (decorator:getProperty tag)
有时候,我们期望修改页面中某个有固定标记的片段,例如我们的jsp中有一个标记<mytag>...</mytag>,此时可以用如下方法实现:在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator
<decorator name="mydecorator2" page="mydecorator2.jsp"> <pattern>/test2.jsp</pattern> </decorator> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <html> <head> <title>My Site - <decorator:title default="Welcome!" /></title> <decorator:head /> </head> <body> <decorator:body /> <decorator:getProperty property="page.content1"/> <decorator:getProperty property="page.content2"/> <!-- do nothing --> <decorator:getProperty property="page.content3"/> <p>This message is in /decorators/mydecorator2.jsp</p> </body> </html> |
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>This is test2</title> </head> <body> <b>This is test2</b> <b>Use <decorator:getProperty> tag</b> <content tag="content1"><p>This is content1</p></content> <content tag="content2"><p>This is content2</p></content> <content tag="content4"><p>This is content4, it shouldn't be display</p></content> </body> </html> |
This is test2 Use <decorator:getProperty> tag This is content1 This is content2 This message is in /decorators/mydecorator2.jsp |
例子3 (page:applyDecorator tag)
在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator<decorator name="mydecorator3" page="mydecorator3.jsp"> <pattern>/test3.jsp</pattern> </decorator> <decorator name="mydecorator31" page="mydecorator31.jsp"> </decorator> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> <html> <head> <title>My Site - <decorator:title default="Welcome!" /></title> <decorator:head /> </head> <body> <decorator:body /> <page:applyDecorator name="mydecorator31"> <content tag="content1"><p>This is content1</p></content> <content tag="content2"><p>This is content2</p></content> </page:applyDecorator> </body> </html> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> <p><i>begin</i></> <decorator:getProperty property="page.content1"/> <decorator:getProperty property="page.content2"/> <p><i>end</i></> |
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>This is test3</title> </head> <body> <b>This is test3</b> <b>Use <page:applyDecorator> tag</b> </body> </html> |
打开浏览器,访问http://localhost:8080/myapp/test3.jsp,将会出现一下内容:
This is test3 Use <page:applyDecorator> tag begin This is content1 This is content2 end |
例子4 (page:param tag)
在{myapp}/WEB-INF/decorators.xml文件中添加以下decorator<decorator name="mydecorator4" page="mydecorator4.jsp"> <pattern>/test4.jsp</pattern> </decorator> <decorator name="mydecorator41" page="mydecorator41.jsp"> </decorator> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> <html> <head> <title>My Site - <decorator:title default="Welcome!" /></title> <decorator:head /> </head> <body> <decorator:body /> <page:applyDecorator name="mydecorator41" > <content tag="content1"><p>This is content1</p></content> <content tag="content2"><p>This is content2</p></content> <page:param name="page.content1"><p>This content1 has been replaced</p></page:param> </page:applyDecorator> </body> </html> |
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> <%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> <p><i>begin</i></> <decorator:getProperty property="page.content1"/> <decorator:getProperty property="page.content2"/> <p><i>end</i></> |
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>This is test4</title> </head> <body> <b>This is test4</b> <b>Use <page:param> tag</b> </body> </html> |
This is test4 Use <page:param> tag begin This content1 has been replaced This is content2 end |
SiteMesh的一个重要特性是使用原始HTML的meta标签(例如<meta name="foo" content="bar">)从基础页面传递信息到装饰器。作为一个例子,下面我们使用一个meta标签来定义HTML页面的作者。
<
html
>
< meta name = " author " content = " test@example.com " >
<
head
>
<
title
>
Simple Document
</
title
>
</
head
>
<
body
>
Hello World
!
<
br
/>
<%=
1
+
1
%>
</
body
>
</
html
>
我们定义一个“smart”装饰器来研究meta标签,如果出现这个标签,则可以得到一个相应的HTML:
<%
@ taglib uri
=
"
sitemesh-decorator
"
prefix
=
"
decorator
"
%>
<
decorator:usePage id
=
"
myPage
"
/>
<
html
>
<
head
>
<
title
>
My Site
-
<
decorator:title
default
=
"
Welcome!
"
/>
</
title
>
<
decorator:head
/>
</
head
>
<
body
>
<
h1
><
decorator:title
default
=
"
Welcome!
"
/></
h1
>
<
h3
>
<
a href
=
"
mailto: <decorator:getProperty property=
" meta.author " default= " staff@example.com "
/>
"
>
<
decorator:getProperty property
=
"
meta.author
"
default
=
"
staff@example.com
"
/>
</
a
>
</
h3
>
<
hr
/>
<
decorator:body
/>
<
p
>
<
small
>
(
<
a href
=
"
/?printable=true
"
>
printable version
</
a
>
)
</
small
>
</
p
>
</
body
>
</
html
>
可以看到我们使用了
getProperty标签的 一个默认属性——如果没有指定author,我们就设定其为staff。如果你决定使用这个模型储存页面的meta数据,你或许需要和你的开发伙伴一起来 确定将使用什么标签以及如何使用他们。简单的,你或许想要使用meta标签来描述诸如页面作者及时间戳之类的东西。更复杂一些,你或许会想像XML文件一 样标准化的管理你的站点导航,同时使用meta标签来通过页面节点转到装饰器。
相关文章推荐
- SWT/Jface 获取OleClientSite中progId的例子
- nRF5 SDK for Mesh(五) 点灯例子
- Unreal4 使用spline , splinemesh组件构建赛道小例子
- D3D Mesh 例子程序
- Unity 几个Mesh特效的例子
- 推荐:使用 Istio Service Mesh 管理微服务的具体例子(译)
- python小例子之4 -- 列表(list)和字典(dict)数据排序
- Java网络编程从入门到精通(32):一个非阻塞I/O的例子
- C++异常处理机制例子
- QBC以及QBE例子
- 【Directx3D游戏开发】——网格模型(mesh .x)
- Linux下tr命令“字符”处理的实用例子
- android binder c++层 - 回调客户端服务 - 客户端(c++层) 调用 服务端(c++层) 例子,服务端回调客户端服务
- 【转】wcf小例子
- HTML5 WebSockets功能的例子
- 在linux内核文件中添加自己的驱动,添加自己的linux驱动,编译自己的linux驱动程序方法和例子图文
- opencv中meanshift和camshift例子的应用
- over() partition by小例子
- 一个很经典的事件冒泡的例子
- 自动注册的例子