1.准备配置文件

英文ApplicationResources_en_US.properties

# Resources for parameter 'com.techson.struts.ApplicationResources'
# Project SSHTemplate
lable.username=Name
lable.password=Password
lable.submit=Submit
lable.link=Link to Sina
lable.email=Email

中文简体

# Resources for parameter 'com.techson.struts.ApplicationResources'
# Project SSHTemplate
lable.username=\u59D3\u540D
lable.password=\u5BC6\u7801
lable.link=\u94FE\u63A5\u5230\u65B0\u6D6A
lable.email=\u90AE\u7BB1
lable.submit=\u63D0\u4EA4
繁体一样。因为编码没有在这儿就不贴出来了

2.UserAction.java

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.techson.struts.action;

import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.techson.struts.form.UserForm;


public class UserAction extends DispatchAction {
    
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        //获取参数
        String lan = request.getParameter("method");
        //浏览器语言
        Locale currentLocale = Locale.getDefault();  
        Enumeration allUserSupportedLocales = request.getLocales();
        
         HttpSession session=request.getSession(false);
         if("cn".equals(lan)){
                
                currentLocale = new Locale("zh", "CN");  
                //session.setAttribute(Globals.LOCALE_KEY, Locale.CHINA);
            }else if("tw".equals(lan)){
                
                currentLocale = new Locale("tw", "CH");
                //session.setAttribute(Globals.LOCALE_KEY, Locale.CHINA);
            }else if("en".equals(lan)){
                currentLocale= new Locale("en", "US");
                //System.out.println(request.getLocale());
            }
         //session.setAttribute(Globals.LOCALE_KEY, currentLocale);
        this.setLocale(request, currentLocale);
        return mapping.findForward("add");
    }
}

注意:配置文件的名称一定要与Action中一一对应,否则是读取不出来的

3.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>

    </head>

    <body>
        <a href="./user.do?method=cn">中文简体</a>
        <br />
        <a href="./user.do?method=tw">中文繁体</a>
        <br />
        <a href="./user.do?method=en">English</a>
        <br />

    </body>
</html>
2.adduser.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html:html lang="en">
<head>
    <title>JSP for UserForm form</title>
</head>
<body>
    <center>
        <html:form action="/user">
            <bean:message key="lable.username" />:<input type="text"
                name="uname" />
            <br />
            <bean:message key="lable.password" />:<input type="text" name="upwd" />
            <br />
            <bean:message key="lable.email" />:<input type="text" name="email" />
            <br />
            <a href="#"><bean:message key="lable.link" />
            </a>
            <br />
            

            <input type="submit" value="<bean:message key='lable.submit'/>">&nbsp;&nbsp;
            
        </html:form>
    </center>
</body>
</html:html>

3.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="userForm" type="com.techson.struts.form.UserForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userForm"
      input="/user/adduser.jsp"
      name="userForm"
      parameter="method"
      path="/user"
      scope="request"
      type="com.techson.struts.action.UserAction">
      <set-property property="cancellable" value="true" />
      <forward name="add" path="/user/adduser.jsp" />
      <forward name="update" path="/user/update.jsp" />
      <forward name="all" path="/user/alluser.jsp" />
    </action>

  </action-mappings>
  <message-resources parameter="com.techson.struts.ApplicationResources" />
</struts-config>

 

posted on 2012-09-02 15:27  Carina_zy  阅读(538)  评论(0编辑  收藏  举报