读书人

struts2国际化配置(+页面上拉修改)

发布时间: 2012-10-14 14:55:08 作者: rapoo

struts2国际化配置(+页面下拉修改)
可以用struts.xml或struts.properties可用于覆盖default.properties的属性配置;
如:
default.properties

### Load custom default resource bundles# struts.custom.i18n.resources=testmessages,testmessages2### workaround for some app servers that don't handle HttpServletRequest.getParameterMap()### often used for WebLogic, Orion, and OC4Jstruts.dispatcher.parametersWorkaround = false

那么我们可以在struts.xml里面这样配置进行配置:
   1. <constant name="struts.custom.i18n.resources" value="messageResources"></constant>  

struts.properties
struts.custom.i18n.resources=messageResources

这两种配置都可以,区别就是在服务器在执行类加载的时候会先加载struts.xml后加载struts.properties
注意:
1。在配置他的资源文件明前缀的时候可以指定在具体的那个目录下面,如果我不是放在src目录下面,而是放在com.test.action的目录下,那么可以这么写
struts.custom.i18n.resources=com.test.action.messageResources
2。在命名的时候要加上区域和本地方言,即在这个action目录下面建立
messageResources_zh_CN.properties
messageResources_en_US.properties
3。在配置中文的时候不能保存那么我们可以在终端里面
执行native2ascii 这个程序来进行转码,转为ascii码
当然,也可以对文件进行转换native2ascii -encoding utf-8 /home/soft22/132.txt /home/soft14/456.txt
<%@ page language="java" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><%@page isELIgnored="false" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <hr> <s:form action="" method="post"> <s:textfield key="login.username"></s:textfield><br> <s:textfield key="login.sex"></s:textfield> <s:submit/><s:reset/> </s:form> </body></html>
然后修改浏览器的使用语言,拦截器就可以修改当前客户端的local属性,服务器会去找相应的配置资源文件信息在客户端显示。

也可以实现页面修改

<%@ 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" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript">function changeLoc(){var s = document.getElementById("loc").value;window.location.href = "hello.action?loc="+s;}</script></head><body><s:select id="loc" name="loc"  onchange="changeLoc()" list='#{"zh_CN":"中文","en_US":"英文"}'></s:select><hr><s:text name="msg.hell"></s:text><br><s:textfield key="user" name="name"></s:textfield><s:fielderror></s:fielderror></body></html>

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="myfirst1" extends="struts-default"><action name="regist" method="{1}"><result name="success">/ok.html</result><result name="input">/regist.jsp</result></action> --><action name="hello" name="code">package tarena.actions;import java.util.Locale;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport{private String loc;public void validate(){System.out.println(loc);String[] code = loc.split("_");Locale locale = new Locale(code[0],code[1]);ActionContext.getContext().setLocale(locale);this.addFieldError("msg",this.getText("msg.error"));}public String execute(){System.out.println(loc);String[] code = loc.split("_");Locale locale = new Locale(code[0],code[1]);ActionContext.getContext().setLocale(locale);return "success";}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}}

读书人网 >软件架构设计

热点推荐