java struts框架,struts2登錄注冊示例_Struts2資源包和本地化示例

 2023-11-19 阅读 27 评论 0

摘要:struts2登錄注冊示例Struts2 framework supports internationalization and we can create resource bundle property files to be used by the framework. Struts2 i18n is used a lot in creating labels based on the locale in result pages using UI tags or to show act

struts2登錄注冊示例

Struts2 framework supports internationalization and we can create resource bundle property files to be used by the framework. Struts2 i18n is used a lot in creating labels based on the locale in result pages using UI tags or to show action messages or errors when action classes implement ValidationAware interface or extend ActionSupport class.

java struts框架。 Struts2框架支持國際化,我們可以創建資源束屬性文件以供框架使用。 Struts2 i18n經常用于使用UI標簽在結果頁面中基于語言環境創建標簽, 或者在操作類實現ValidationAware接口或擴展ActionSupport類時顯示操作消息或錯誤 。

Struts2 framework supports i18n through I18nInterceptor interceptor and we can pass locale in request with parameter request_locale. This interceptor is part of defaultStack interceptor stack, so we don’t need to do anything for localization. However we can override locale request parameter with parameterName parameter, we will look into this in our sample project.

Struts2框架通過I18nInterceptor攔截器支持i18n,我們可以在請求中使用參數request_locale傳遞語言環境。 該攔截器是defaultStack攔截器堆棧的一部分,因此我們無需為本地化做任何事情。 但是,我們可以使用parameterName參數覆蓋語言環境請求參數,我們將在示例項目中對此進行研究。

struts2的執行流程。When we pass localization key, Struts 2 framework looks for resource bundles at various places in below order:

當我們傳遞本地化密鑰時,Struts 2框架將按以下順序在各個位置查找資源包:

  • {ActionClassName}.properties and it should be in the same package with Action Class.

    {ActionClassName} .properties,它應該與Action Class放在同一程序包中。
  • Interface.properties (every interface and sub-interface)

    Interface.properties(每個接口和子接口)
  • BaseClass.properties (all the way to Object.properties)

    BaseClass.properties(一直到Object.properties)
  • ModelDriven’s model (if implements ModelDriven)

    ModelDriven的模型(如果實現ModelDriven)
  • package.properties in the class package and then to the parent packages till the root

    將class.package中的package.properties,然后到父包,直到根
  • global resource properties configured in struts property file

    在struts屬性文件中配置的全局資源屬性

It’s good to have options but excessive options can lead to confusion. Also if we have so many property files for localization, it will add IO cost in searching the keys into them. Personally I always create only package and global properties files but sometimes when we need to show message specific to an action class, action class property files come handy.

有選擇權很好,但是過多的選擇權會導致混亂。 同樣,如果我們有太多要本地化的屬性文件,它將在搜索密鑰時增加IO成本。 我個人始終只創建包和全局屬性文件,但是有時當我們需要顯示特定于操作類的消息時,操作類屬性文件會派上用場。

We can get localized text by following options:

我們可以通過以下選項獲取本地化的文本:

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

Above method can be used when action class is extending ActionSupport class, getText() method is defined in ActionSupport class.

當操作類擴展ActionSupport類時,可以使用上述方法,在ActionSupport類中定義了getText()方法。

<s:text name="key">Default Text</s:text>

We can use Struts text tag to get the localized string, if the key is not found then “Default Text” will be used.

我們可以使用Struts文本標簽來獲取本地化的字符串,如果找不到該鍵,則將使用“默認文本”。

<s:i18n name="global"><s:text name="key"></s:text>
</s:i18n>

We can use i18n tag to specify the location of resource bundle from where the properties will be loaded. In above case, it will try to get key value from global.properties from WEB-INF/classes directory. If we know the location of keys, then it’s good to use i18n tag for better performance.

我們可以使用i18n標記來指定從中加載屬性的資源束的位置。 在上述情況下,它將嘗試從WEB-INF / classes目錄的global.properties中獲取鍵值。 如果我們知道鍵的位置,那么最好使用i18n標簽以獲得更好的性能。

<s:textfield key="mykey" name="textfieldName"/>

Most of the UI tags have key attribute that we can use to get the localized text from resource bundle.

大多數UI標簽都具有key屬性,我們可以使用它來從資源包中獲取本地化的文本。

Let’s look into a simple web application where we are using Struts2 resource bundle and i18n support for generating localized response. Our final project will look like below image.

讓我們看一個簡單的Web應用程序,其中我們使用Struts2資源包和i18n支持來生成本地化響應。 我們的最終項目將如下圖所示。

For our example, we will have localized texts for fr_FR and de_DE locales and we have ActionClass, package and global resource bundles. As you can see from above image that Action Class property files are in the same package whereas package property files are in the parent package. Global resource bundles are in the classes directory and we will configure it in struts configuration file.

對于我們的示例,我們將為fr_FR和de_DE語言環境提供本地化的文本,并具有ActionClass,程序包和全局資源包。 從上圖可以看出,動作類屬性文件在同一程序包中,而程序包屬性文件在父程序包中。 全局資源包位于classes目錄中,我們將在struts配置文件中對其進行配置。

配置文件 (Configuration Files)

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>Struts2Localization</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

pom.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Struts2Localization</groupId><artifactId>Struts2Localization</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>2.3</version><configuration><warSourceDirectory>WebContent</warSourceDirectory><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins><finalName>${project.artifactId}</finalName></build><dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.15.1</version></dependency></dependencies>
</project>

Deployment descriptor and maven pom.xml files are simple and used to configure web application to use Struts2 framework.

部署描述符和maven pom.xml文件很簡單,用于將Web應用程序配置為使用Struts2框架。

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""https://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- constant to define result path locations to project root directory --><constant name="struts.convention.result.path" value="/"></constant><!-- constant to define global resource bundle --><constant name="struts.custom.i18n.resources" value="global"></constant><package name="user" namespace="/" extends="struts-default"><action name="home"><result>/home.jsp</result></action><action name="welcome" class="com.journaldev.struts2.actions.WelcomeAction"><interceptor-ref name="defaultStack"><param name="i18n.parameterName">appLocale</param></interceptor-ref><result name="success">/welcome.jsp</result></action></package></struts>

We are providing global resource bundle name as global and overriding the i18n interceptor locale request parameter to appLocale, we will use this in our request form parameters for our use case.

我們提供的全局資源包名稱為global,并將i18n攔截器語言環境請求參數覆蓋到appLocale,我們將在我們的用例的請求表單參數中使用此名稱。

動作班 (Action Class)

package com.journaldev.struts2.actions;import com.opensymphony.xwork2.ActionSupport;public class WelcomeAction extends ActionSupport {@Overridepublic String execute() {return SUCCESS;}private String username;private String address;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

Action class is simple and have only few java bean properties with getter and setter methods.

動作類很簡單,只有幾個帶有getter和setter方法的Java bean屬性。

資源包 (Resource Bundles)

Global Resource Bundles

全球資源包

global.properties

global.properties

#global labels
global.username=User Name
global.address=Address
global.submit=Submit
global.selectlocale=Select Localeaction.welcome.thankyou=Thank You

global_fr_FR.properties

global_fr_FR.properties

#global labels
global.username=User Name (FR)
global.address=Address (FR)
global.submit=Submit (FR)
global.selectlocale=Select Locale (FR)action.welcome.thankyou=Thank You (FR)

global_de_DE.properties

global_de_DE.properties

#global labels
global.username=User Name (DE)
global.address=Address (DE)
global.submit=Submit (DE)
global.selectlocale=Select Locale (DE)action.welcome.thankyou=Thank You (DE)

Package Resource Bundles

軟件包資源包

package.properties

package.properties

action.welcome.title=Welcome Page
action.welcome.thankyou=Thank You!!

package_fr_FR.properties

package_fr_FR.properties

action.welcome.title=Welcome Page (FR)
action.welcome.thankyou=Thank You!! (FR)

package_de_DE.properties

package_de_DE.properties

action.welcome.title=Welcome Page (DE)
action.welcome.thankyou=Thank You!! (DE)

Action Class Resource Bundles

動作類資源包

WelcomeAction.properties

WelcomeAction.properties

#action specific labels
action.welcome.username=User Name
action.welcome.address=Address

WelcomeAction_fr_FR.properties

WelcomeAction_fr_FR.properties

#action specific labels for locale fr_FR
action.welcome.username=User Name (FR)
action.welcome.address=Address (FR)

WelcomeAction_de_DE.properties

WelcomeAction_de_DE.properties

#action specific labels for locale de_DE
action.welcome.username=User Name (DE)
action.welcome.address=Address (DE)

JSP頁面 (JSP Pages)

home.jsp

home.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Struts2 Localization Example</title>
</head>
<body>
<s:form action="welcome">
<s:textfield key="global.username" name="username"></s:textfield>
<s:textfield key="global.address" name="address"></s:textfield>
<s:select list="{'en_US','fr_FR','de_DE'}" name="appLocale" key="global.selectlocale"></s:select>
<s:submit key="global.submit" name="submit"></s:submit>
</s:form>
</body>
</html>

As you can see that we can provide locale from the select box in home.jsp and the variable name is same as configured in struts property file for locale parameter.

如您所見,我們可以從home.jsp的選擇框中提供語言環境,變量名稱與在struts屬性文件中為locale參數配置的名稱相同。

welcome.jsp

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:property value="getText('action.welcome.title')"/></title>
</head>
<body>
<s:property value="getText('action.welcome.username')"/>: <s:property value="username"/><br>
<s:text name="action.welcome.address"></s:text>: <s:property value="address"/><br><br><s:i18n name="global"><s:text name="action.welcome.thankyou"></s:text>
</s:i18n>
</body>
</html>

welcome.jsp is showing usage of localization keys through i18n, text and property tag.

welcome.jsp通過i18n,文本和屬性標簽顯示了本地化密鑰的用法。

Now when we run our application, we get following response pages.

現在,當我們運行應用程序時,我們將獲得以下響應頁面。

That’s all for Struts2 internationalization and resource bundle example, it’s very simple and easy to use. Make sure you have properly designed the application resource bundles otherwise it will become hard to maintain if the number of files are too much.

這就是Struts2國際化和資源包示例的全部,它非常簡單易用。 確保正確設計了應用程序資源包,否則,如果文件數量過多,將很難維護。

Download project from below link and play around with it for better understanding.

從下面的鏈接下載項目,并進行嘗試以更好地理解。

Download Struts2 Localization Example Project下載Struts2本地化示例項目

翻譯自: https://www.journaldev.com/2304/struts2-resource-bundles-and-localization-example

struts2登錄注冊示例

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/183336.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息