JSP页面:
localeAction.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Action中的国际化</title> </head> <body> <font color="red">${message}</font> <s:form action="local.do"> <s:textfield name="msg" label="%{getText('login.name')}" /> <s:submit value="%{getText('login.submit')}" /> </s:form> </body> </html>
属性文件:global_en_US.properties
login.name=UserName login.submit=Sure login.succmessage=SUCCESS login.errmessage=ERROR
属性文件:global_zh_CN.properties
login.name=\u7528\u6237\u540D (login.name=用户名) login.submit=\u63D0\u4EA4 (login.submit=提交) login.succmessage=\u6210\u529F (login.succmessage=成功) login.errmessage=\u5931\u8D25 (login.errmessage=失败)
struts.properties属性文件中添加下面配置:
struts.custom.i18n.resources=global
需要加入国际化信息的Action
LocalAction.java
import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class LocalAction extends ActionSupport { private String msg; private String message; public String execute() throws Exception { if (msg.equalsIgnoreCase("tom_seed")) { message = getText("login.succmessage"); } else { message = getText("login.errmessage"); } return SUCCESS; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
在struts.xml文件中配置Action
<action name="local" class="Struts.action.LocalAction"> <result>/localeAction.jsp</result> </action>
在struts.properties文件中配置
struts.locale=en_US(或者zh_CN)
不同的浏览器语言环境有不同的显示信息;