Java的Struts框架中的主题模板和国际化设置_MySQL
主题模板 如果不指定一个主题,然后Struts2中会使用默认的XHTML主题。例如Struts 2中选择标签: <s:textfield name="name" label="Name" /> 生成HTML标记: <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.properties struts.ui.theme属性(默认为XHTML) 以下语法指定他们在标签级别,如果愿意为不同的标签使用不同的主题: <s:textfield name="name" label="Name" theme="xhtml"/> 因为它不是非常实用,每个标签的基础上使用主题,所以干脆我们可以指定规则struts.properties文件中使用以下标签: # 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 以下的结果是,我们拿起从本地化章节里我们使用默认设置struts.ui.theme= XHTML的struts-default.properties文件中,默认情况下,在 struts2-core.xy.z.jar文件,这是由主题。 主题如何工作的? Struts 2 tags + Associated template file = Final HTML markup code. 默认模板已经写在FreeMarker和他们有扩展名 .ftl。可以设计使用速度或JSP模板,并据此设置配置在使用struts.ui.templateSuffix 和 struts.ui.templateDir struts.properties。 创建新的主题: 我们要修改现有的默认模板XHTML学习目的。所以,现在让,我们复制内容从 struts2-core-x.y.z.jar/template/xhtml 到我们的主题目录,并只修改WebContent/WEB-INF/classes/template/mytheme/control.ftl文件。当我们打开control.ftl 它将有下面几行: <table class="${parameters.cssClass?default('wwFormTable')?html}"<#rt/><#if parameters.cssStyle??> style="${parameters.cssStyle?html}"<#rt/></#if>> 让我们上述文件control.ftl改变有以下内容: <table style="border:1px solid black;"> 如果检查看 form.ftl 会发现,control.ftl 这个文件中,form.ftl这个文件是指从XHTML主题。因此,让我们改变如下: <#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" /> 我假设不会有太多了解FreeMarker模板语言,仍然寻找FTL文件需要做什么,可以得到一个不错的主意。然而,让我们除上述变动外,并回到我们的本地化的例子,创建 WebContent/WEB-INF/classes/struts.properties 档案的以下内容: # Customized themstruts.ui.theme=mytheme# Directory where theme template residesstruts.ui.templateDir=template# Sets the template type to ftl.struts.ui.templateSuffix=ftl 现在这种变化后,右键点击项目名称,并单击Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2。这会给出以下画面: XHTML主题复制后的变化,我们做了主题这是一个结果,可以看到一个表单组件周围的边框。 FreeMarker学习,如果你努力,那么将能够创建或修改主题很容易。至少现在,你必须有一个基本的了解Sturts2主题和模板。 本地化/国际化 Struts2提供本地化,即,国际化(i18n)支持,通过资源包,拦截器和标签库在以下地方:
资源包: 简单的命名格式的资源文件是: bundlename_language_country.properties 当引用消息元素,其关键,按照下列顺序进行相应的消息包的Struts框架搜索:
多语言开发应用程序,就必须保持相应的到那些语言/区域设置多个属性文件定义的键/值对中的所有内容。例如,如果要开发应用程序(默认)为美国英语,西班牙语,和法语就必须创建三个属性文件。在这里,我将使用只global.properties文件,你可以利用不同的属性文件来隔离不同类型的消息。
访问消息: 要显示i18n的文本,使用的调用属性标记gettext,或其他任何标记,例如UI标签如下: <s:property value="getText('some.key')" /> 文本标记检索从默认的资源包,即一个消息 struts.properties <s:text name="some.key" /> i18n标签推值栈上的任意资源束。 i18n标签范围内的其他标签可以显示该资源包的消息: <s:i18n name="some.package.bundle"> <s:text name="some.key" /></s:i18n> 大多数UI标签的键属性,可以用来检索的消息,从一个资源包: <s:textfield key="some.key" name="textfieldName"/> Localization 例子: <%@ 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> 我们将创建的success.jsp文件,该文件将被调用的情况下定义的动作返回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> 在这里,我们需要创建两个动作。 (一)第一个动作一个Locale和照顾,用不同的语言显示相同的index.jsp文件(二)另一项行动是为了照顾提交表单本身。的动作都将返回SUCCESS,但我们会采取不同的动作,返回值的基础上,因为我们的目的是不同的两个动作: 动作处理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; }} 现在。让我们创建以下三个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 我们将创建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文件中的内容: <?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> 现在,右键点击项目名称,并单击 Export > WAR File创建一个WAR文件。然后部署此WAR在Tomcat的webapps目录下。最后,启动Tomcat服务器和尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp。这会给出以下画面: 现在选择的任何一种语言,让我们说,我们选择西班牙语,这将显示以下结果: 您可以尝试用法语。最后,让我们尝试点击“Submit ”按钮,当我们在西班牙语言,它会显示以下画面: 恭喜你,现在有一个多语种的网页,可以在全球范围内启动您的网站。 |