zoukankan      html  css  js  c++  java
  • Struts1实现国际化,实现多种语言(English,简体,繁体)

    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>

  • 相关阅读:
    Why Choose Jetty?
    Jetty 的工作原理以及与 Tomcat 的比较
    Tomcat设计模式
    Servlet 工作原理解析
    Tomcat 系统架构
    spring boot 打包方式 spring boot 整合mybaits REST services
    wireshark udp 序列号 User Datagram Protocol UDP
    Maven 的聚合(多模块)和 Parent 继承
    缓存策略 半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完全自动化的框架我更喜欢mybatis
    Mybatis解决sql中like通配符模糊匹配 构造方法覆盖 mybits 增删改
  • 原文地址:https://www.cnblogs.com/zyfxlv/p/2667572.html
Copyright © 2011-2022 走看看