layout: post
title: struts2实验3:struts2 国际化
categories: STRUTS2
description: struts2实验3:struts2 国际化
keywords: STRUTS2
p.s
关键在于struts.xml中
<constant name="struts.custom.i18n.resources" value="mess"/>
和mess_en_US.properties 与 mess_zh_CN.properties
jsp调用properties数据
<jsp:useBean id="d" class="java.util.Date" scope="page"/>
<s:text name="welcomeMsg">
<s:param>${d} </s:param>
</s:text>
或
<s:textfield name="user.username" key="username" value="testuser"/>
中的key即代表properties中的键
在Action中调用properties中的键值使用getText("properties中的键"),详见UserLoginAction中的login()
目录结构
代码(只粘贴了部分关键代码类,下文有GITHUB链接)
UserLoginAction
package com.zy.javaee.controller.actions;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zy.javaee.controller.BaseAction;
import com.zy.javaee.domin.User;
/**
* Created by Joey_Tsai on 2018/3/19.
*/
public class UserLoginAction extends BaseAction {
private User user;
private User defaultUser;
public String userLoginPage(){
setDirectUrl("/html/userLoginPage.jsp");
return SUCCESS;
}
public String login(){
ActionContext actionContext = ActionContext.getContext();
if (getUser().getUsername().equals(getDefaultUser().getUsername()) && getUser().getPassword().equals(getDefaultUser().getPassword())) {
actionContext.getSession().put("user",user);
actionContext.put("tip",getText("succTip"));
setDirectUrl("/html/helloPage.jsp");
return SUCCESS;
} else {
actionContext.put("tip",getText("failTip"));
return userLoginPage();
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public User getDefaultUser() {
return defaultUser;
}
public void setDefaultUser(User defaultUser) {
this.defaultUser = defaultUser;
}
}
userLoginPage
<%--
Created by IntelliJ IDEA.
User: Joey_Tsai
Date: 2018/3/19
Time: 14:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>普通用户登陆界面</title>
</head>
<body>
<a href="/login">普通用户登陆</a>
<a href="/admin/login_adminLoginPage">管理员登陆</a>
<h1>普通用户登陆</h1>
<!-------------------- 国际化 --------------------->
<a href="/login.do?request_locale=zh_CN">中文</a>
<a href="/login.do?request_locale=en_US">English</a>
<!----------------------------------------------------->
<br/>
${requestScope.tip}
<jsp:useBean id="d" class="java.util.Date" scope="page"/>
<s:text name="welcomeMsg">
<s:param>${d} </s:param>
</s:text>
<s:form action="/users/login_login" method="POST">
<table>
<tr>
<td><s:textfield name="user.username" key="username" value="testuser"/>
</td>
</tr>
<tr>
<td><s:password name="user.password" key="password" value="123"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><s:submit value="登录"/></td>
</tr>
</table>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.action.extension" value="do,action,"/>
<constant name="struts.serve.static.browserCache" value="false"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- ################################ 国际化重点 -Start- ################################-->
<constant name="struts.custom.i18n.resources" value="mess"/>
<!-- ################################ 国际化重点 -End- ################################-->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="exception" >/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="tryException" class="com.zy.javaee.controller.actions.ErrorProcessAction" method="tryException">
<result name="success">/users/login_userLoginPage.do</result>
</action>
</package>
<package name="default" namespace="/" extends="struts-global">
<action name="login">
<result type="redirect">/users/login_userLoginPage</result>
</action>
</package>
<package name="admin" namespace="/admin" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.AdminLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultAdmin.username">testadmin</param>
<param name="defaultAdmin.password">123</param>
</action>
</package>
<package name="users" namespace="/users" extends="struts-global">
<action name="login_*" class="com.zy.javaee.controller.actions.UserLoginAction" method="{1}">
<result name="success">${directUrl}</result>
<param name="defaultUser.username">testuser</param>
<param name="defaultUser.password">123</param>
</action>
</package>
</struts>