Struts 2 Customize Theme & Freemarker Template 客制佈景與模板 說明了Struts2 的 tags 是可以自行客制成自已想要的佈景,現在來說明如何使用 FreeMarker 來制作 Struts2 屬於自已建作的 tag libs.
工具:
eclipse : Version: Mars Release (4.5.0) Build id: 20150621-1200
pom.xml:
struts2-core: 2.5.8 struts-annotations: 1.0.5 (不可以用 1.0.6 會出現 tlibVersion Missing 的錯誤) commons-logging: 1.2 jsp-api : 2.2 javax.servlet-api : 4.0.1 |
Step 01: 建立一個空的 maven project
Step 02: 建立客制的 class: HtmlTag.java 在 package com.easyui.struts2.views.jsp.ui 下
package com.easyui.struts2.views.jsp.ui; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ui.AbstractClosingTag; import com.easyui.struts2.components.Html; import com.opensymphony.xwork2.util.ValueStack; public class HtmlTag extends AbstractClosingTag { @Override public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Html(stack, req, res); } } |
Step 03 : 建立客制的 class: Html.java 在 package com.easyui.struts2.components , 此檔主要是要產生 .tld 的標籤描述檔, 以及告訴 freemarker 要讀取那一個 ftl , 此範例是要有 html.ftl 與 html-close.ftl 這兩個檔案.
package com.easyui.struts2.components; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.ClosingUIBean; import org.apache.struts2.views.annotations.StrutsTag; import com.opensymphony.xwork2.util.ValueStack; @StrutsTag( name="html", tldTagClass="com.easyui.struts2.views.jsp.ui.HtmlTag", description="建立一個空的 HTML 結構", allowDynamicAttributes=true) public class Html extends ClosingUIBean { public Html(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { super(stack, request, response); } @Override public String getDefaultOpenTemplate() { return "html"; } @Override protected String getDefaultTemplate() { return "html-close"; } } |
註:
1. 在 pom.xml 中需加入 struts-annotations: 1.0.5 才可以使用 annotations 描述
2. @StrutsTag: annotation for marking a Struts tag
3. @StrutsTagAttribute: annotation for marking an attribute of a Struts tag
Step 04: 建立此 tags (標籤) 的 templates & theme
1. 建立 package: template.easyui 並撰寫 html.ftl 與 html-close.ftl
2. 建立 空的 package: template.easyui.html 用來存放 Tobago plugin 自動產生的相關樣版檔案
html.ftl 檔
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>${parameters.title}</title> </head> <body> |
html-close.ftl 檔
</body> </html> |
Step 05: 用 Tobago plugin 建立 TLD file 及使用 Maven 產生這個 taglib 的 jar 檔
請看附註二
Step 06: 佈署 (Deploy taglib in your Web application) , 將這個 taglib 的 jar 檔放在 WEB-INF/lib 下
記得要在 struts.xml 裡加入 <constant name="struts.ui.theme" value="easyui"/> , 這樣才會去讀取 JAR 檔 template.easyui 裡的 ftl 檔案
Step 07: JSP 裡使用客製的 tag libs
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="i" uri="/esayui-tags" %> <i:html title="Basic Layout - jQuery EasyUI Demo"> hi easyui-tags <s:form action="Login"> <s:textfield key="username"/> <s:password key="password" /> <s:submit/> </s:form> </i:html> |
附註:
一、 Struts2 taglibs Introduction
Few words about the elements of the Struts2 UI component framework should help to understand better the scenario.
JSP Tag: implementation of the JSP tag (e.g TextFieldTag). Notice Struts2 has also Velocity and FreeMarker tags
UI Component: actual implementation of the behavior of the component (e.g TextField)
Template: FreeMarker template which renders the html markup (e.g. <input type=”text”….)
Theme: collection of templates (e.g. simple or ajax)
二、整個專案結構如下
註:
1. 在 pom.xml 中需加入 tobago 描述, 才會自動產生目錄: src/main/resources/META-INF 的 easyui-tags.tld 描述檔及 package: template.easyui.html 裡的 html.html
2. 目前 的編譯器 complier 需用 JDK 1.7.x 版才會成功, JDK1.8.x 會無法產生 easyui-tags.tld 描述檔 及相關的樣版檔
3. Tobago plugin 在 pom.xml 的設定如下:
<plugin> <groupId>org.apache.myfaces.tobago</groupId> <artifactId>maven-apt-plugin</artifactId> <configuration> <A> uri=/esayui-tags,tlibVersion=${project.version},jspVersion=2.0,shortName=easyui,displayName="EasyUI Tags", outFile=${basedir}/src/main/resources/META-INF/${project.artifactId}.tld, description="EasyUI Tags", outTemplatesDir=${basedir}/src/main/java/template/easyui/html </A> <resourceTargetPath>target</resourceTargetPath> <fork>false</fork> <force>true</force> <nocompile>true</nocompile> <showWarnings>true</showWarnings> <factory> org.apache.struts.annotations.taglib.apt.TLDAnnotationProcessorFactory </factory> <target>1.5</target> <includes> <include>**/*.java</include> </includes> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>execute</goal> </goals> </execution> </executions> </plugin> |
三、 完整的 pom.xml 檔
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gu.apps</groupId> <artifactId>easyui-tags</artifactId> <version>1.0-SNAPSHOT</version> <name>easyui-tags</name> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.24.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-annotations</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.myfaces.tobago</groupId> <artifactId>maven-apt-plugin</artifactId> <configuration> <A> uri=/esayui-tags,tlibVersion=${project.version},jspVersion=2.0,shortName=easyui,displayName="EasyUI Tags", outFile=${basedir}/src/main/resources/META-INF/${project.artifactId}.tld, description="EasyUI Tags", outTemplatesDir=${basedir}/src/main/java/template/easyui/html </A> <resourceTargetPath>target</resourceTargetPath> <fork>false</fork> <force>true</force> <nocompile>true</nocompile> <showWarnings>true</showWarnings> <factory> org.apache.struts.annotations.taglib.apt.TLDAnnotationProcessorFactory </factory> <target>1.5</target> <includes> <include>**/*.java</include> </includes> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>execute</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <finalName>easyui-tags</finalName> <defaultGoal>install</defaultGoal> </build> </project> |
參考:
https://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/
留言列表