Simple realization of Struts2 internationalization i18n

Internationalization (i18n for short) means that the program can be used in different language environments without any modification. Its implementation is based on the browser's support settings for language options and is not commonly used in general projects. . For similar internationalization effects, please go to the official website of MyBatis .


Action class

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String execute() {
		// 从国际化资源配置中读取信息键对应的值
		String loginMsg = this.getText("action.login.loginMsg", new String[] { userName });
		ActionContext.getContext().getSession().put("message", loginMsg);
		return "success";
	}

}

English language resource configuration

#Login header prompt text
index.form.tabHead=Hello, please login!
#User name input
index.form.userName=User name
#The password input
index.form.userPwd=User password
#The submit button
index.form.btnSubmit=log in
#Action request data
action.login.loginMsg=Login successfully, {0}, the page content has been internationalized for you!
#Welcome page i18n of interface content
welcome.content.prompt=login successfully! Active user: {0}.

Chinese language resource allocation

#Login header prompt text
index.form.tabHead=\u4F60\u597D\uFF0C\u8BF7\u767B\u5F55\uFF01
#User name input
index.form.userName=\u7528\u6237\u540D
#The password input
index.form.userPwd=\u5BC6\u7801
#The submit button
index.form.btnSubmit=\u767B\u5F55
#Action request data
action.login.loginMsg=\u767B\u5F55\u6210\u529F\uFF0C{0}\uFF0C\u9875\u9762\u5185\u5BB9\u5DF2\u4E3A\u60A8\u56FD\u9645\u5316\u663E\u793A\uFF01
#Welcome page i18n of interface content
welcome.content.prompt=\u767B\u5F55\u6210\u529F\uFF01 \u5F53\u524D\u7528\u6237\uFF1A{0}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!-- 注册全局范围资源文件,键出自default.properties,值出自资源文件基本名 -->
	<constant name="struts.custom.i18n.resources" value="baseName"/>
	<package name="strutsI18n" namespace="/i18n" extends="struts-default">
		<action name="loginAction" class="action.LoginAction">
			<result name="success">/welcome.jsp</result>
		</action>
		<!-- action中不指明class属性代表继承了ActionSupport的类默认执行execute() -->
		<action name="language">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 注册Struts2的启动项(过滤器) -->
	<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>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Index</title>
	</head>
	<body>
	
		<ul style="float: left">
			<li>
				<a href="${pageContext.request.contextPath}/i18n/language.action?request_locale=zn_CN">简体中文</a>
			</li>
			<li>
				<a href="${pageContext.request.contextPath}/i18n/language.action?request_locale=en_US">English</a>
			</li>
		</ul>
		
		<br /><br /><br />
		
		<s:form action="i18n/loginAction" method="post">
			<table style="margin: 50px auto; border: 1px solid purple; text-align: center;">
				<tr>
					<td colspan="2"><s:text name="index.form.tabHead"/></td>
				</tr>
				<tr>
					<td>
						<s:textfield name="userName" key="index.form.userName"  />
					</td>
				</tr>
				<tr>
					<td><s:textfield name="userPwd" key="index.form.userPwd"  /></td>
				</tr>
				<tr>
					<td colspan="2"><s:submit key="index.form.btnSubmit" /></td>
				</tr>
			</table>
		</s:form>
	
	</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Welcome</title>
	</head>
	<body>
	
		<h2 align="center">
			<!-- name会将值栈中的对应值转交给国际化属性文件指定键中的占位符 -->
			<s:text name="welcome.content.prompt">
				<s:param><s:property value="userName" /></s:param>
			</s:text>
		</h2>
		<h3 align="center">${sessionScope.message}</h3>
	
	</body>
</html>

Run example
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44965393/article/details/112059899