主題模板
如果不指定一個主題,然后Struts2中會使用默認(rèn)的XHTML主題。例如Struts 2中選擇標(biāo)簽:
<s:textfield name="name" label="Name" />
生成HTML標(biāo)記:
<tr><td class="tdLabel"> <label for="empinfo_name" class="label">Name:</label></td><td> <input type="text" name="name" value="" id="empinfo_name"/></td></tr>
這里empinfo struts.xml文件中定義動作名稱。
選擇主題:
可以指定主題Struts 2每一個標(biāo)簽的基礎(chǔ)上或指定的主題Struts 2使用,可以使用下列方法之一:
在struts.properties struts.ui.theme屬性(默認(rèn)為XHTML)
以下語法指定他們在標(biāo)簽級別,如果愿意為不同的標(biāo)簽使用不同的主題:
<s:textfield name="name" label="Name" theme="xhtml"/>
因?yàn)樗皇欠浅?shí)用,每個標(biāo)簽的基礎(chǔ)上使用主題,所以干脆我們可以指定規(guī)則struts.properties文件中使用以下標(biāo)簽:
# Standard UI themestruts.ui.theme=xhtml# Directory where theme template residesstruts.ui.templateDir=template# Sets the default template type. Either ftl, vm, or jspstruts.ui.templateSuffix=ftl
以下的結(jié)果是,我們拿起從本地化章節(jié)里我們使用默認(rèn)設(shè)置struts.ui.theme= XHTML的struts-default.properties文件中,默認(rèn)情況下,在 struts2-core.xy.z.jar文件,這是由主題。

主題如何工作的?
對于一個給定的主題,每一個struts標(biāo)簽有關(guān)聯(lián)的模板,如:s:textfield -> text.ftl 和 s:password -> password.ftl等,這些模板文件來壓縮struts2-core.xy.z.jar文件。這些模板文件保持一個預(yù)先定義的HTML布局為每個標(biāo)簽。所以Struts2 框架生成最終的HTML標(biāo)記代碼使用Sturts標(biāo)簽和相關(guān)的模板。
Struts 2 tags + Associated template file = Final HTML markup code.
默認(rèn)模板已經(jīng)寫在FreeMarker和他們有擴(kuò)展名 .ftl。可以設(shè)計(jì)使用速度或JSP模板,并據(jù)此設(shè)置配置在使用struts.ui.templateSuffix 和 struts.ui.templateDir struts.properties。
創(chuàng)建新的主題:
最簡單的方法來創(chuàng)建一個新的主題是復(fù)制現(xiàn)有的任何主題/模板文件,并做必要的修改。所以,讓我們開始創(chuàng)建一個文件夾 WebContent/WEB-INF/classes 名為模板和子文件夾與我們新的主題的名稱,例如WebContent/WEB-INF/classes/template/mytheme。從這里,可以從頭開始構(gòu)建模板,或者可以復(fù)制模板從Struts2分布和根據(jù)需要進(jìn)行修改。
我們要修改現(xiàn)有的默認(rèn)模板XHTML學(xué)習(xí)目的。所以,現(xiàn)在讓,我們復(fù)制內(nèi)容從 struts2-core-x.y.z.jar/template/xhtml 到我們的主題目錄,并只修改WebContent/WEB-INF/classes/template/mytheme/control.ftl文件。當(dāng)我們打開control.ftl 它將有下面幾行:
<table class="${parameters.cssClass?default('wwFormTable')?html}"<#rt/><#if parameters.cssStyle??> style="${parameters.cssStyle?html}"<#rt/></#if>>讓我們上述文件control.ftl改變有以下內(nèi)容:
<table style="border:1px solid black;">
如果檢查看 form.ftl 會發(fā)現(xiàn),control.ftl 這個文件中,form.ftl這個文件是指從XHTML主題。因此,讓我們改變?nèi)缦拢?/p>
<#include "/${parameters.templateDir}/xhtml/form-validate.ftl" /><#include "/${parameters.templateDir}/simple/form-common.ftl" /><#if (parameters.validate?default(false))> onreset="${parameters.onreset?default('clearErrorMessages(this); clearErrorLabels(this);')}"<#else> <#if parameters.onreset??> onreset="${parameters.onreset?html}" </#if></#if>><#include "/${parameters.templateDir}/mytheme/control.ftl" /> 我假設(shè)不會有太多了解FreeMarker模板語言,仍然尋找FTL文件需要做什么,可以得到一個不錯的主意。然而,讓我們除上述變動外,并回到我們的本地化的例子,創(chuàng)建 WebContent/WEB-INF/classes/struts.properties 檔案的以下內(nèi)容:
# Customized themstruts.ui.theme=mytheme# Directory where theme template residesstruts.ui.templateDir=template# Sets the template type to ftl.struts.ui.templateSuffix=ftl
現(xiàn)在這種變化后,右鍵點(diǎn)擊項(xiàng)目名稱,并單擊Export > WAR File創(chuàng)建一個WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2。這會給出以下畫面:

XHTML主題復(fù)制后的變化,我們做了主題這是一個結(jié)果,可以看到一個表單組件周圍的邊框。 FreeMarker學(xué)習(xí),如果你努力,那么將能夠創(chuàng)建或修改主題很容易。至少現(xiàn)在,你必須有一個基本的了解Sturts2主題和模板。
本地化/國際化
國際化(i18n)是規(guī)劃和實(shí)施的產(chǎn)品和服務(wù),使他們能很容易地適應(yīng)特定的本地語言和文化的過程中,這個過程被稱為本地化。國際化的過程有時也被稱為翻譯或本地化啟用。國際化是縮寫i18n,因?yàn)槲液蛢啥擞胣字打頭,并有18個字符之間的第i個和最后n。
Struts2提供本地化,即,國際化(i18n)支持,通過資源包,攔截器和標(biāo)簽庫在以下地方:
資源包:
Struts2 使用資源包來提供Web應(yīng)用程序的用戶多語言和區(qū)域選項(xiàng)。不必?fù)?dān)心在不同的語言編寫的網(wǎng)頁。所有必須做的是創(chuàng)造一個資源包為每個想要的語言。資源包將包含標(biāo)題,消息和其他文本的語言用戶。資源包的文件,該文件包含鍵/值對您的應(yīng)用程序的默認(rèn)語言。
簡單的命名格式的資源文件是:
bundlename_language_country.properties
這里,軟件包可以ActionClass,接口,超類,型號,封裝,全球資源屬性。接下來的部分 language_country ,En_US的等在這里,可以跳過這是可選的全國部分區(qū)域表示es_ES和英語(美國),西班牙語(西班牙)表示語言環(huán)境的語言環(huán)境,例如代表國家。
當(dāng)引用消息元素,其關(guān)鍵,按照下列順序進(jìn)行相應(yīng)的消息包的Struts框架搜索:
多語言開發(fā)應(yīng)用程序,就必須保持相應(yīng)的到那些語言/區(qū)域設(shè)置多個屬性文件定義的鍵/值對中的所有內(nèi)容。例如,如果要開發(fā)應(yīng)用程序(默認(rèn))為美國英語,西班牙語,和法語就必須創(chuàng)建三個屬性文件。在這里,我將使用只global.properties文件,你可以利用不同的屬性文件來隔離不同類型的消息。
訪問消息:
有幾種方法可以訪問的信息資源,包括gettext的,文本標(biāo)簽,UI標(biāo)簽的關(guān)鍵屬性,國際化標(biāo)簽。讓我們來看看他們簡單:
要顯示i18n的文本,使用的調(diào)用屬性標(biāo)記gettext,或其他任何標(biāo)記,例如UI標(biāo)簽如下:
<s:property value="getText('some.key')" />文本標(biāo)記檢索從默認(rèn)的資源包,即一個消息 struts.properties
<s:text name="some.key" />
i18n標(biāo)簽推值棧上的任意資源束。 i18n標(biāo)簽范圍內(nèi)的其他標(biāo)簽可以顯示該資源包的消息:
<s:i18n name="some.package.bundle"> <s:text name="some.key" /></s:i18n>
大多數(shù)UI標(biāo)簽的鍵屬性,可以用來檢索的消息,從一個資源包:
<s:textfield key="some.key" name="textfieldName"/>
Localization 例子:
創(chuàng)建的index.jsp從前一章到多種語言。相同的文件將被寫入,如下所示:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Employee Form with Multilingual Support</title></head><body> <h1><s:text name="global.heading"/></h1> <s:url id="indexEN" namespace="/" action="locale" > <s:param name="request_locale" >en</s:param> </s:url> <s:url id="indexES" namespace="/" action="locale" > <s:param name="request_locale" >es</s:param> </s:url> <s:url id="indexFR" namespace="/" action="locale" > <s:param name="request_locale" >fr</s:param> </s:url> <s:a href="%{indexEN}" >English</s:a> <s:a href="%{indexES}" >Spanish</s:a> <s:a href="%{indexFR}" >France</s:a> <s:form action="empinfo" method="post" namespace="/"> <s:textfield name="name" key="global.name" size="20" /> <s:textfield name="age" key="global.age" size="20" /> <s:submit name="submit" key="global.submit" /> </s:form></body></html>我們將創(chuàng)建的success.jsp文件,該文件將被調(diào)用的情況下定義的動作返回SUCCESS。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Success</title></head><body> <s:property value="getText('global.success')" /></body></html>在這里,我們需要創(chuàng)建兩個動作。 (一)第一個動作一個Locale和照顧,用不同的語言顯示相同的index.jsp文件(二)另一項(xiàng)行動是為了照顧提交表單本身。的動作都將返回SUCCESS,但我們會采取不同的動作,返回值的基礎(chǔ)上,因?yàn)槲覀兊哪康氖遣煌膬蓚€動作:
動作處理locale:
package com.yiibai.struts2;import com.opensymphony.xwork2.ActionSupport;public class Locale extends ActionSupport{ public String execute() { return SUCCESS; }}提交表單處理動作:
package com.yiibai.struts2;import com.opensymphony.xwork2.ActionSupport;public class Employee extends ActionSupport{ private String name; private int age; public String execute() { return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}現(xiàn)在。讓我們創(chuàng)建以下三個global.properties文件放在CLASSPATH中:
GLOBAL.PROPERTIES:global.name = Nameglobal.age = Ageglobal.submit = Submitglobal.heading = Select Localeglobal.success = Successfully authenticatedGLOBAL_FR.PROPERTIES:global.name = Nom d'utilisateur global.age = l'âgeglobal.submit = Soumettre desglobal.heading = Sé lectionnez Localglobal.success = Authentifi é avec succèsGLOBAL_ES.PROPERTIES:global.name = Nombre de usuarioglobal.age = Edadglobal.submit = Presentarglobal.heading = seleccionar la configuracion regionalglobal.success = Autenticado correctamente
我們將創(chuàng)建struts.xml中兩個動作如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="global" /> <package name="helloworld" extends="struts-default" namespace="/"> <action name="empinfo" class="com.yiibai.struts2.Employee" method="execute"> <result name="input">/index.jsp</result> <result name="success">/success.jsp</result> </action> <action name="locale" class="com.yiibai.struts2.Locale" method="execute"> <result name="success">/index.jsp</result> </action> </package></struts>
以下是web.xml文件中的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
現(xiàn)在,右鍵點(diǎn)擊項(xiàng)目名稱,并單擊 Export > WAR File創(chuàng)建一個WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:

現(xiàn)在選擇的任何一種語言,讓我們說,我們選擇西班牙語,這將顯示以下結(jié)果:

您可以嘗試用法語。最后,讓我們嘗試點(diǎn)擊“Submit ”按鈕,當(dāng)我們在西班牙語言,它會顯示以下畫面:

恭喜你,現(xiàn)在有一個多語種的網(wǎng)頁,可以在全球范圍內(nèi)啟動您的網(wǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選