如何在OpenCms中進(jìn)行 JSP 模板開(kāi)發(fā)
(JSP template development Howto)
使用JSP進(jìn)行OpenCms模板開(kāi)發(fā) template development with JSP
建立需要的模板頁(yè)是網(wǎng)站建設(shè)的一個(gè)非常重要的特性。用OpenCms生成的站點(diǎn)建立在使用多個(gè)模板的基礎(chǔ)上,這些模板定義了統(tǒng)一的呈現(xiàn)內(nèi)容的設(shè)計(jì)。
因?yàn)閹缀跛械墓こ潭夹枰@個(gè)核心功能,所以必須認(rèn)真學(xué)習(xí)這個(gè)文檔。通過(guò)以下步驟學(xué)習(xí),你將能建立自己的模板。
一、建立一個(gè)簡(jiǎn)單的JSP模板
A simple JSP template
A "JSP template" in OpenCms is just a normal JSP page
that uses special tags to include content at specified positions. For
this simple example we will start to develop a JSP template which adds
some tags around the content, like <html> or <body>.
This is the JSP template
/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto.jsp:
<%@ taglib prefix="cms" uri="http://www./taglib/cms" %>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><cms:property name="Title" escapeHtml="true" /></title>
<meta HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html;
CHARSET=<cms:property name="content-encoding" default="ISO-8859-1"
/>">
<link type= "text/css" href="<cms:link>../resources/mystyle.css</cms:link>">
</head>
<body>
<h2>My first template head</h2>
<!-- Main pagebody starts here -->
<cms:include element= "body"/>
<!--Main pagebody ends here -->
<h2>My first template foot</h2>
</body>
</html>
The taglib directive for the OpenCms taglib description
<%@ taglib prefix="cms" uri="http://www./taglib/cms" %>
must be declared first before you can use any of the
OpenCms tags. For a detailed description of the OpenCms taglib, please
refer to the OpenCms taglib documentation which is available as a
separate module.
Note that the directory where to place a JSP template in
a module is a subfolder called templates. Only if the template is
stored there, it can be found in the template selection list while
creating a new page.
注意存放JSP模板的目錄在 module的templates的文件夾,當(dāng)模板存儲(chǔ)在這里時(shí),就能在創(chuàng)建新頁(yè)面的模板選擇中找到他
In this example, the title of the page is read from the
"title" property of the OpenCms page and placed in the body of the HTML
<title> tags. The content encoding of the page is defined in the
same way.
在這個(gè)例子中,頁(yè)面的標(biāo)題從OpenCms頁(yè)面的title屬性中讀出,然后放置在HTML<title>標(biāo)簽中。頁(yè)面的內(nèi)容用同樣的方式定義。
The stylesheet is put into the <cms:link> tag. In
JSP templates, the path to the stylesheet can be a relative path from
the location of the template. It is another convention to create a
resources/ subfolder in the module to store all kind of resources like
style sheets and images there that belong to the templates of the
module. Again, this is optional, but we think it's a good idea.
格式表存在<cms:link>tag中,在JSP模板中,格式表(stylesheet)的目錄是和模板的目錄相關(guān)的。一般會(huì)新建一個(gè)resources文件夾存放所有的資源,如樣式表,圖片,屬于模塊中的模板。
Note : You can use your CSS stylesheet in the Editor by
attaching a property "template" to the JSP template. The value of the
property must be the filename (including the full path) of your
stylesheet.
注意:你可以用你的CSS樣式表在編輯器中加入一個(gè)template屬性給JSP 模板。屬性的值必須是包含完全路徑的文件名。
The most important line is the <cms:include
element="body"> tag which includes the editable body element of the
page. Please refer to the OpenCms taglib documentation for all options
of the <cms:include> tag.
最重要的行是<cms:include
element="body">標(biāo)簽,它包含了可以編輯的頁(yè)面體元素。參見(jiàn)OpenCms taglib documentation for
all options of the <cms:include> tag.
Let's have a look at the page example-simplepage.html
that is build with the described OpenCms master template, which is
called "Alkacon documentation howto simple template". You can also
create new pages with this template.
Exercise: Create a page that uses this template and edit
the content of the page in the editor. Have a look at the page preview.
Then try to modify the template's HTML. Perhaps create a new template
with a different layout.
Congratulations! You have succeeded in creating your first simple JSP template in OpenCms.
Building a complete JSP template
在OpenCms中建立完整的JSP模板
If you have created a new page that uses the simple
template from step 1, you will have found out that you can edit the
content of the page and that edited content is displayed on the page if
you open it in the preview or the browser. It works perfect for editable
HTML pages.
如果你已經(jīng)創(chuàng)建用了使用簡(jiǎn)單模板的新頁(yè)面,你將發(fā)現(xiàn)你可以編輯出現(xiàn)在頁(yè)面上的內(nèi)容,如果你在瀏覽器上預(yù)覽,它能很好的作為Html頁(yè)面運(yùn)轉(zhuǎn)
In
this section we will show you how to create a "complete" template. This
is a template that can be used for dynamic JSP as well as for pages
editable with the WYSIWYG editor.
在這里我將像你展示如何建立一個(gè)完整的模板,這個(gè)模板能用于動(dòng)態(tài)JSP頁(yè)面,也能用于WYSIWYG編輯器
The example JSP example-jsp-simple.jsp is a simple form. Have a look at the JSP source code:
<%@ page session="false" %>
<html>
<body>
<h1>A simple form</h1>
<%
String name = request.getParameter("name");
if (name != null) {
%>
<h2>Your name is: <%= name %></h2>
<% } %>
<form name="test" method="get" action="example-jsp-simple.jsp">
<p>Enter
your name: <input name="name" size="20"
value=""> <input type="submit"
value="OK"></p>
</form>
</body>
</html>
It would be handy using the same template which works
for HTML pages as well as for dynamic JSP pages, to get the same layout.
To achieve this, the JSP template from the previous example must be
extended with <cms:template> tags. Again, please refer to the
OpenCms taglib documentation for all options of the <cms:template>
tag.
將相同的模板用于HTML頁(yè)面是方便的,如同用動(dòng)態(tài)JSP頁(yè)面。為了得到相同的展示,之前例子的JSP模板必須擴(kuò)展<cms:template>tags,請(qǐng)參加OpenCms taglib文檔中關(guān)于<cms:template>的標(biāo)志。
Here is the "complete" version of the simple template from the previous example. You will find the source code at
/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto-complete.jsp.
<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www./taglib/cms" %>
<cms:template element="head">
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><cms:property name="Title" escapeHtml="true" /></title>
<meta
HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; CHARSET=<cms:property
name="content-encoding" default="ISO-8859-1" />">
<link type="text/css" rel="stylesheet" href="<cms:link>../resources/mystyle.css</cms:link>">
</head>
<body>
<h2>My first template head</h2>
<!-- Main page body starts here -->
</cms:template><cms:template element="body">
<cms:include element="body" />
</cms:template><cms:template element="foot">
<!-- Main page body ends here -->
<h2>My first template foot</h2>
</body>
</html>
</cms:template>
As
you can see, the only changes are the additional <cms:template>
tags. These tags are required so that the template is usable from a JSP.
They are ignored by the pages that are edited with the WYSIWYG editor.
Assure that all parts of the JSP template are enclosed by
<cms:template> tags.
正如你看到的,唯一的改變是增加的<cms:template>標(biāo)志。這些
tag是必須的,這樣template可以用于JSP。他們被編輯所見(jiàn)即所得的頁(yè)所忽略。確認(rèn),JSP
template的所有部分必須包含在<cms:template>標(biāo)記中。
To use this adapted template, you just have to add 3 additional lines to your JSP form:
為了使用這個(gè)適合的template,你只需要增加這3個(gè)附加行到你的JSP列中
<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www./taglib/cms" %>
<cms:include property="template" element="head" />
<h1>A simple form</h1>
<%
String name = request.getParameter("name");
if (name != null) {
%>
<h2>Your name is: <%= name %></h2>
<% } %>
<form name="test" method="get" action="example-jsp-template.jsp">
<p>Enter your name: <input name="name" size="20" value="">
<input type="submit" value="OK"></p>
</form>
<cms:include property="template" element="foot" />
The JSP includes the "head" and "foot" element of the JSP template specified in the "template" property of the JSP.
JSP包含JSP模板的“head”和“foot”,這些屬性在JSP“template”屬性中指定。
The
last thing required for this template to work correctly is that you
must attach a property "template" to the JSP. The value of the property
must be the filename (including path) of your JSP template, in our
example
/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto-complete.jsp.
Actually, you can name the property anything you want because the name
is choosen in the property="template" part of the <cms:include>
tag, but we think the name "template" is a good choice for selecting the
template.
最后,你必須將”template”屬性附給JSP這樣才能使這個(gè)模板正常運(yùn)行,屬性的值必須是JSP模板的文件名(包括路徑)。在我們的例子中
/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto-complete.jsp.
實(shí)際上你可以給你你的屬性起任何名字,因?yàn)槊质强蛇x的在<cms:include>標(biāo)簽的(property=”template”)中,但是我們認(rèn)為把選擇模板命名為”template”是一個(gè)好的選擇。
Note:
Instead of attaching the property "template" to every JSP individually,
you could also attach it to a parent folder. A JSP that does not have
the "template" property set will inherit it from its parent folder.
注:代替將template屬性賦給每一個(gè)JSP。你也可以付給它的父文件夾。沒(méi)有設(shè)置模板屬性的JSP將從它的父文件模板。
The example example-jsp-template.jsp shows the form with the included elements of the JSP template.
Exercise: Create a JSP using the elements of the shown
JSP template howto-complete.jsp. Include the elements several times in
various order and look at the output. Try to create your own complete
JSP template and define different <cms:template> parts. Create a
new JSP and include your self-defined parts. Also try to use this
complete template for editable pages in the WYSIWYG editor.
Congratulations! You have succeeded in creating your first "complete" OpenCms JSP template.
OpenCms模塊的文件夾結(jié)構(gòu)
2006-07-14 13:48
模塊的文件夾結(jié)構(gòu)
當(dāng)建立一個(gè)模塊后,建立模塊的骨架結(jié)構(gòu)就自動(dòng)建立了,模塊的主目錄放在/system/modules/folder下,并且給你的文件一個(gè)包名。如你的
模塊名為 com.life.site.模塊的文件夾在./system/modules/com.life.site/
模塊主目錄包括一些子目錄,系統(tǒng)自動(dòng)認(rèn)為這些目錄是模塊的一部分,并自動(dòng)和模塊一起導(dǎo)出。模塊的子目錄可以分為,system,convention,custom
Module "system" folders
模塊”system”文件夾是有固定名字的文件夾,這樣opencms可以找到這些系統(tǒng)文件,這些文件夾是
/system/modules/org.opencms.mymodule/default_bodies/
/system/modules/org.opencms.mymodule/templates/
default_bodies/ subfolder包含帶用于”body”部分的 html
片斷(fragments)的opencms XML
Templates。把些文件放入default_bodies/目錄,可以加入預(yù)處理頁(yè)面設(shè)計(jì)(pre-defined page
layouts)加入你的opencms安裝(installation)例如圖表設(shè)計(jì) 。default
bodies的標(biāo)題展示在擁有此權(quán)限用戶的新頁(yè)面(new page)對(duì)話框的" Copy content from
"復(fù)制內(nèi)容的下拉框中。這樣你可以隱藏default bodies,通過(guò)設(shè)置用戶或組的讀取標(biāo)志設(shè)為v
templates/
文件夾里是opencms的模板。在用戶建立新頁(yè)面的時(shí)候,OpenCms在所有安裝的模塊的templates文件夾中尋找模板templates。這
樣所有templates的標(biāo)題都可以在新建文件的”Templates”下拉框中展示給用戶。在此過(guò)程中也會(huì)檢測(cè)用戶的權(quán)限,所以你可以隱藏
templates用”v”標(biāo)識(shí)
如果你也添加了classes/folder ,lib/folder在模塊建立的時(shí)候。這些目錄也會(huì)建立。
/system/modules/org.opencms.mymodule/classes/com/opencms/mymodule/
/system/modules/org.opencms.mymodule/lib/
在classes文件夾中,建立一個(gè)你模塊名在package
明的文件夾結(jié)構(gòu)。所有的class中的subfoldesrs合他們的內(nèi)容會(huì)導(dǎo)出至WEB-INF/classes
當(dāng)你模塊發(fā)布的時(shí)候。例如,你可以放置一個(gè)本地的Java 資源包在
classes/org/opencms/mymodule/my.properties中,在lib/文件夾中你可以放裝入你的模塊的JAR。所有的
lib的子文件夾在你的模塊發(fā)布的時(shí)候?qū)?huì)導(dǎo)出至WEB-INF/lib/文件夾
模塊”convention”文件夾
你必須手動(dòng)添加“convention”文件夾到你的模塊。盡管你可以添加任何子文件夾到你的模塊,并起你喜歡的任何名字。但是我們建議你還是遵守這些習(xí)俗,我們已經(jīng)確定了模塊的功能性,這樣使其他人能容易的明白你的模塊。
以下文件夾認(rèn)為是convention文件夾
/system/modules/org.opencms.mymodule/resources/
/system/modules/org.opencms.mymodule/elements/
在resources文件夾下放置images,javascripts和其他資源像用于你的模板的Html,CSS 樣式表,
Elements文件夾放置高級(jí)JSP和傳統(tǒng)的XML template 如navigation
以下為原文
The folder structure of a module
When you create a
new module, a skeleton folder structure is automatically created for
your module. The main module directory will be placed in the
/system/modules/ folder and it will have the package name of your
module, so if your module is named org.opencms.mymodule the main module
folder will be /system/modules/org.opencms.mymodule/.
The main module folder can contain several subfolders.
All of these subfolders will be considered part of the module and
automatically exported (or deleted) with the module. The subfolders of a
module can be divided in three categories: "system" folders,
"convention" folders and "custom" folders.
Module "system" folders
Module "system" folders are
folders that require a fixed name so that OpenCms can find it's bits and
bytes at the right place. These folders are:
/system/modules/org.opencms.mymodule/default_bodies/
/system/modules/org.opencms.mymodule/templates/
The
default_bodies/ subfolder contains OpenCms XMLTemplates with HTML
fragments for the "body" section of your pages. By putting files in
default_bodies/, you can add pre-defined page layouts (e.g. complex
table layouts) to your OpenCms installation. The titles of the default
bodies found are displayed at the "new page" dialog in the "Copy content
from" selectbox that is shown to the user. User permissions are also
checked during this process, so that you can hide default bodies with
the "v" flag for certain users or groups.
The templates/ subfolder
contains OpenCms templates. When a user creates a new page, OpenCms
checks the templates/ subfolder of all installed modules to search for
templates. The titles of the templates found are displayed at the "new
page" dialog in the "Template" selectbox that is shown to the user. User
permissions are also checked during this process, so that you can hide
templates with the "v" flag for certain users or groups.
In case you either added the classes/folder and/or the lib/ folder on module creation, these folders are additionally created:
/system/modules/org.opencms.mymodule/classes/com/opencms/mymodule/
/system/modules/org.opencms.mymodule/lib/
Inside
the classes/ subfolder, an entire subfolder structure with the package
name of your module is created. All subfolders of the classes/ subfolder
and their contents will be exported to WEB-INF/classes/ when your
module is published. For example you could place a localization Java
resource bundle in the module subfolder
classes/org/opencms/mymodule/my.properties.
In the lib/ subfolder
you could place any Java archives (JAR) that are to be shipped with your
module. All contents of the lib/ subfolder will be exported to
WEB-INF/lib/ when your module is published.
Module "convention" folders
Module
"convention" folders are folders that you must add manually to your
module. You are free to create any subfolders in your modules and name
them as you like. However, we suggest you to follow the conventions
outlined here that we have established for certain module
functionalities. This will make it a lot easier for other people to
understand your module if you ever contribute modules to the OpenCms
community.
The following module folders are considered to be "convention" folders:
/system/modules/org.opencms.mymodule/resources/
/system/modules/org.opencms.mymodule/elements/
The
resources/ subfolder by convention keeps any images, JavaScripts and
other static resources like CSS style sheets that are part of the HTML
of your templates or otherwise required by your module.
The
elements/ subfolder by convention is used for the storage of further JSP
or legacy XML template elements, such as navigation elements, forms
etc.
As said already, it is not a system requirement that these
folders are named this way, but we suggest you follow these conventions.
Module "custom" folders
Any other subfolders you
create for your module are considered to be "custom" module folders. How
much of these "custom" folders a module has is entirely up to the
module developer.