struts2的实现方法

LangSelector.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <script type="text/javascript">
<!--
    function langSelecter_onChanged() {
        document.langForm.submit();
    }
//-->
</script>
    </head>
    <body>
        <s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']" />
        <s:bean id="locales" name="tutorial.Locales" />
        <form action="&lt;s:url includeParams="get" encode="true"/>"
            name="langForm"
            style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;"&gt;
            Language:
            <s:select label="Language" list="#locales.locales" listKey="value"
                listValue="key"
                value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
                name="request_locale" id="langSelecter"
                onchange="langSelecter_onChanged()" theme="simple" />
        </form>
    </body>
</html>

HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>Hello World</title>
        <s:include value="/LangSelector.jsp"/>
    </head>
    <body>
        <h2>
            <s:text name="HelloWorld" />
        </h2>
        <h2>
            <s:property value="%{getText('HelloWorld')}" />
        </h2>
    </body>
</html>

Locales.java

package tutorial;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
public class Locales {
   public Map<String, Locale> getLocales() {
       Map<String, Locale> locales =new Hashtable<String, Locale>(2);
       locales.put("American English", Locale.US);
       locales.put("Simplified Chinese", Locale.CHINA);
       return locales;
   }
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="Struts2_test" extends="struts-default">
        <action name="HelloWorld">
            <result>/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

struts.properties

struts.custom.i18n.resources=globalMessages

globalMessages_en_US.properties

HelloWorld=Hello World!

globalMessages_zh_CN.properties

HelloWorld=你好!

http://localhost:9000/struts2_local/HelloWorld.action访问

struts1的实现方法

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="userName" type="java.lang.String" />
    </form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="loginForm"
      input="/form/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="com.mingda.struts.action.LoginAction">
      <forward name="success" path="/form/success.jsp" />
    </action>
  </action-mappings>
  <message-resources parameter="com.mingda.struts.ApplicationResources" />
</struts-config>

/form/login.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
    <head>
        <title>JSP for DynaActionForm form</title>
    </head>
    <body>
        <html:form action="/login">
            <bean:message key="language.choose"/><br>
            <html:link  action="login.action?locale=zh"><bean:message key="language.zh"/></html:link><br>
            <html:link  action="login.action?locale=en"><bean:message key="language.en"/></html:link><br>
            <html:submit/><html:cancel/>
        </html:form>
    </body>
</html>

/form/success.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
    <head>
        <title>JSP for DynaActionForm form</title>
    </head>
    <body>
        <bean:message key="login.username"/>
    </body>
</html>

LoginAction.java

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.mingda.struts.action;
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;
/**
* MyEclipse Struts
* Creation date: 01-22-2008
*
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
* @struts.action-forward name="success" path="/form/success.jsp"
*/
public class LoginAction extends Action {
    /*
     * Generated Methods
     */
    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        String locale = request.getParameter("locale");
        if(locale!=null)
        {
            Locale locale_language=null;
            if(locale.equalsIgnoreCase("zh"))
                locale_language = Locale.SIMPLIFIED_CHINESE;
            else if(locale.equalsIgnoreCase("en"))
                locale_language = Locale.ENGLISH;
            request.getSession().setAttribute(Globals.LOCALE_KEY,locale_language);
        }
        return mapping.findForward("success");
    }
}

ApplicationResources_zh_CN.properties

login.username=用户名
language.choose=选择语言
language.zh=简体中文
language.en=英语

 

ApplicationResources.properties

# Resources for parameter 'com.mingda.struts.ApplicationResources'
# Project test_struts1
login.username=UserName
language.choose=Choose Language
language.zh=simplified_chinese
language.en=english