欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java的Struts框架中的主題模板和國(guó)際化設(shè)置

 更新時(shí)間:2015年12月03日 17:50:52   投稿:goldensun  
這篇文章主要介紹了Java的Struts框架中的主題模板和國(guó)際化設(shè)置,Struts是Java的SSH三大web開放框架之一,需要的朋友可以參考下

主題模板

如果不指定一個(gè)主題,然后Struts2中會(huì)使用默認(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文件中定義動(dòng)作名稱。

選擇主題:
可以指定主題Struts 2每一個(gè)標(biāo)簽的基礎(chǔ)上或指定的主題Struts 2使用,可以使用下列方法之一:

  • 主題屬性的具體標(biāo)簽
  • 主題屬性標(biāo)簽的周邊表單標(biāo)簽
  • 頁面范圍的屬性,名為“主題”
  • 請(qǐng)求范圍屬性名為“主題”
  • 會(huì)話作用域?qū)傩悦麨椤爸黝}”
  • 應(yīng)用程序作用域的屬性命名為“主題”

在struts.properties struts.ui.theme屬性(默認(rèn)為XHTML)

以下語法指定他們?cè)跇?biāo)簽級(jí)別,如果愿意為不同的標(biāo)簽使用不同的主題:

<s:textfield name="name" label="Name" theme="xhtml"/>

因?yàn)樗皇欠浅?shí)用,每個(gè)標(biāo)簽的基礎(chǔ)上使用主題,所以干脆我們可以指定規(guī)則struts.properties文件中使用以下標(biāo)簽:

# Standard UI theme
struts.ui.theme=xhtml
# Directory where theme template resides
struts.ui.templateDir=template
# Sets the default template type. Either ftl, vm, or jsp
struts.ui.templateSuffix=ftl

以下的結(jié)果是,我們拿起從本地化章節(jié)里我們使用默認(rèn)設(shè)置struts.ui.theme= XHTML的struts-default.properties文件中,默認(rèn)情況下,在 struts2-core.xy.z.jar文件,這是由主題。

2015123174205904.png (551×328)

主題如何工作的?
對(duì)于一個(gè)給定的主題,每一個(gè)struts標(biāo)簽有關(guān)聯(lián)的模板,如:s:textfield -> text.ftl 和 s:password -> password.ftl等,這些模板文件來壓縮struts2-core.xy.z.jar文件。這些模板文件保持一個(gè)預(yù)先定義的HTML布局為每個(gè)標(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??梢栽O(shè)計(jì)使用速度或JSP模板,并據(jù)此設(shè)置配置在使用struts.ui.templateSuffix 和 struts.ui.templateDir struts.properties。

創(chuàng)建新的主題:
最簡(jiǎn)單的方法來創(chuàng)建一個(gè)新的主題是復(fù)制現(xiàn)有的任何主題/模板文件,并做必要的修改。所以,讓我們開始創(chuàng)建一個(gè)文件夾 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 會(huì)發(fā)現(xiàn),control.ftl 這個(gè)文件中,form.ftl這個(gè)文件是指從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è)不會(huì)有太多了解FreeMarker模板語言,仍然尋找FTL文件需要做什么,可以得到一個(gè)不錯(cuò)的主意。然而,讓我們除上述變動(dòng)外,并回到我們的本地化的例子,創(chuàng)建 WebContent/WEB-INF/classes/struts.properties 檔案的以下內(nèi)容:

# Customized them
struts.ui.theme=mytheme
# Directory where theme template resides
struts.ui.templateDir=template
# Sets the template type to ftl.
struts.ui.templateSuffix=ftl

現(xiàn)在這種變化后,右鍵點(diǎn)擊項(xiàng)目名稱,并單擊Export > WAR File創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2。這會(huì)給出以下畫面:

2015123174541042.png (550×315)

XHTML主題復(fù)制后的變化,我們做了主題這是一個(gè)結(jié)果,可以看到一個(gè)表單組件周圍的邊框。 FreeMarker學(xué)習(xí),如果你努力,那么將能夠創(chuàng)建或修改主題很容易。至少現(xiàn)在,你必須有一個(gè)基本的了解Sturts2主題和模板。

本地化/國(guó)際化
國(guó)際化(i18n)是規(guī)劃和實(shí)施的產(chǎn)品和服務(wù),使他們能很容易地適應(yīng)特定的本地語言和文化的過程中,這個(gè)過程被稱為本地化。國(guó)際化的過程有時(shí)也被稱為翻譯或本地化啟用。國(guó)際化是縮寫i18n,因?yàn)槲液蛢啥擞胣字打頭,并有18個(gè)字符之間的第i個(gè)和最后n。

Struts2提供本地化,即,國(guó)際化(i18n)支持,通過資源包,攔截器和標(biāo)簽庫在以下地方:

  • UI 標(biāo)簽
  • 消息和錯(cuò)誤
  • 動(dòng)作類

資源包:
Struts2 使用資源包來提供Web應(yīng)用程序的用戶多語言和區(qū)域選項(xiàng)。不必?fù)?dān)心在不同的語言編寫的網(wǎng)頁。所有必須做的是創(chuàng)造一個(gè)資源包為每個(gè)想要的語言。資源包將包含標(biāo)題,消息和其他文本的語言用戶。資源包的文件,該文件包含鍵/值對(duì)您的應(yīng)用程序的默認(rèn)語言。

簡(jiǎn)單的命名格式的資源文件是:

bundlename_language_country.properties
這里,軟件包可以ActionClass,接口,超類,型號(hào),封裝,全球資源屬性。接下來的部分 language_country ,En_US的等在這里,可以跳過這是可選的全國(guó)部分區(qū)域表示es_ES和英語(美國(guó)),西班牙語(西班牙)表示語言環(huán)境的語言環(huán)境,例如代表國(guó)家。

當(dāng)引用消息元素,其關(guān)鍵,按照下列順序進(jìn)行相應(yīng)的消息包的Struts框架搜索:

  • ActionClass.properties
  • Interface.properties
  • SuperClass.properties
  • model.properties
  • package.properties
  • struts.properties
  • global.properties

多語言開發(fā)應(yīng)用程序,就必須保持相應(yīng)的到那些語言/區(qū)域設(shè)置多個(gè)屬性文件定義的鍵/值對(duì)中的所有內(nèi)容。例如,如果要開發(fā)應(yīng)用程序(默認(rèn))為美國(guó)英語,西班牙語,和法語就必須創(chuàng)建三個(gè)屬性文件。在這里,我將使用只global.properties文件,你可以利用不同的屬性文件來隔離不同類型的消息。

  • global.properties: 默認(rèn)情況下,英語(美國(guó))將被應(yīng)用
  • global_fr.properties: 這將是法語環(huán)境中使用。
  • global_es.properties: 這將被用于西班牙語言環(huán)境。

訪問消息:
有幾種方法可以訪問的信息資源,包括gettext的,文本標(biāo)簽,UI標(biāo)簽的關(guān)鍵屬性,國(guó)際化標(biāo)簽。讓我們來看看他們簡(jiǎn)單:

要顯示i18n的文本,使用的調(diào)用屬性標(biāo)記gettext,或其他任何標(biāo)記,例如UI標(biāo)簽如下:

<s:property value="getText('some.key')" />

文本標(biāo)記檢索從默認(rèn)的資源包,即一個(gè)消息 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)簽的鍵屬性,可以用來檢索的消息,從一個(gè)資源包:

<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)用的情況下定義的動(dòng)作返回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>

在這里,我們需要?jiǎng)?chuàng)建兩個(gè)動(dòng)作。 (一)第一個(gè)動(dòng)作一個(gè)Locale和照顧,用不同的語言顯示相同的index.jsp文件(二)另一項(xiàng)行動(dòng)是為了照顧提交表單本身。的動(dòng)作都將返回SUCCESS,但我們會(huì)采取不同的動(dòng)作,返回值的基礎(chǔ)上,因?yàn)槲覀兊哪康氖遣煌膬蓚€(gè)動(dòng)作:

動(dòng)作處理locale:

package com.yiibai.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Locale extends ActionSupport{
  public String execute() 
  {
    return SUCCESS;
  }
}

提交表單處理動(dòng)作:

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)建以下三個(gè)global.properties文件放在CLASSPATH中:

GLOBAL.PROPERTIES:
global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success = Successfully authenticated
GLOBAL_FR.PROPERTIES:
global.name = Nom d'utilisateur 
global.age = l'âge
global.submit = Soumettre des
global.heading = Sé lectionnez Local
global.success = Authentifi é avec succès
GLOBAL_ES.PROPERTIES:
global.name = Nombre de usuario
global.age = Edad
global.submit = Presentar
global.heading = seleccionar la configuracion regional
global.success = Autenticado correctamente

我們將創(chuàng)建struts.xml中兩個(gè)動(dòng)作如下:

<?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)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會(huì)給出以下畫面:

2015123174838040.png (556×329)

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

2015123174858699.png (559×331)

您可以嘗試用法語。最后,讓我們嘗試點(diǎn)擊“Submit ”按鈕,當(dāng)我們?cè)谖靼嘌勒Z言,它會(huì)顯示以下畫面:

2015123174917989.png (560×333)

恭喜你,現(xiàn)在有一個(gè)多語種的網(wǎng)頁,可以在全球范圍內(nèi)啟動(dòng)您的網(wǎng)站。

相關(guān)文章

最新評(píng)論