1、国际化资源包
application_en.properties
#Application Resources for the "Hello" sample application #Application Resources that are specific to the hello.jsp file hello.jsp.title=Hello - A first Struts program hello.jsp.page.heading=Hello World! A first Struts application hello.jsp.prompt.person=Please enter a UserName to say hello to : hello.jsp.page.hello=Hello #Validation and error messages for HelloForm.java and HelloAction.java hello.dont.talk.to.monster=We don't want to say hello to Monster!!! hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
application_zh.properties
#Application Resources for the "Hello" sample application #Application Resources that are specific to the hello.jsp file hello.jsp.title=\u4f60\u597d\uff0c\u7b2c\u4e00\u4e2astruts\u9879\u76ee hello.jsp.page.heading=\u793e\u4f1a\u4f60\u597d\uff0c\u6211\u7684\u7b2c\u4e00\u4e2astruts\u9879\u76ee hello.jsp.prompt.person=\u8bf7\u5199\u4e0b\u4f60\u7684\u540d\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0e\u4ed6\u6253\u62db\u547c hello.jsp.page.hello=\u4f60\u597d #Validation and error messages for HelloForm.java and HelloAction.java hello.dont.talk.to.monster=\u6211\u4eec\u4e0d\u80fd\u5bf9Monster\u8bf4\u4f60\u597d hello.no.username.error=\u8bf7\u8f93\u5165\u59d3\u540d
2、action
package hello; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class LocalAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String language=request.getParameter("language"); //获取当前语言国际化 //Locale locale=request.getLocale(); //new locale对象 Locale newlocale=new Locale(language,""); //覆盖session中国际化对象 request.getSession(true).setAttribute(Globals.LOCALE_KEY, newlocale); return mapping.findForward("success"); } }
3、struts-config.xml配置
<action path="/localAction" type="hello.LocalAction" scope="request" > <forward name="success" path="/hello.jsp"></forward> </action>
4、JSP
<h3>Language Options</h3> <ul> <li><html:link action="localAction?language=en">English</html:link></li> <li><html:link action="localAction?language=zh">China</html:link></li> </ul>