struts2学习笔记之十(用户选择语言环境小程序)

struts 默认使用浏览器设置选择国家和语言,提供机制让用户选择国家语言环境 根据request_locale来确定。

如在提交请求时加上request_locale=en_US来设定语言环境为English
例如:http://localhost:8080/I18n/I18nJSP?request_locale=zh_CN
这样就会得到中文的语言环境显示JSP页面

接下来演示一个用户在JSP页面选择语言环境的小程序

  • web.xml配置
  • 制作JSP页面
  • Action处理类
  • struts.xml配置
web.xml配置

使用空项目的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<s:i18n name="/resource/selectlanguage">
<html>
  <head>      
    <title><s:text name="LanguageEnvironment" /></title>
  </head>
  <s:bean var="selectlanguage" name="org.struts2.bean.LanguageEnvironmentList"/>
  <body>
    <s:form action="selectlanguage">
        <s:select id="sl" list="#selectlanguage.localeList" 
            listKey="value"
            listValue="key"         
            key="selectLanguageEnvironment"
            name="request_locale"
            onchange="this.form.submit();"
        />
    </s:form>
  </body>
  <script type="text/javascript">
        document.getElementById("sl").value="${param.request_locale}";
  </script>
</html>
</s:i18n>

这里的form的action不能写成”#”(至少我在实验的时候不能用),否则的话会出错,查看网页源代码显示提交的页面是#.action

Action处理类

在这个项目里,用户没有提交过来什么数据,Action也不需要返回逻辑视图,因此这个项目不需要编写Action。但是却需要一个Bean,用于返回系统中的系统中可选的语言环境

package org.struts2.bean;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class LanguageEnvironmentList {

    private Map<String,Locale> localeList;

    {
        localeList = new HashMap<String,Locale>();
        localeList.put(Locale.CHINA.getDisplayLanguage(Locale.CHINA), Locale.CHINA);
        localeList.put(Locale.US.getDisplayLanguage(Locale.US),Locale.US);
    }

    public Map<String,Locale> getLocaleList() {
        return localeList;
    }

}

Locale.CHINA.getDisplayLanguage(Locale.CHINA)表示返回中文环境的显示;
Locale.CHINA中文环境。

struts.xml配置

使用的是一个自己制作的空项目的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- 开发模式为true,出错的时候信息很多,或者配置文件修改会自动备份,减少重启服务器 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 编码字符集,应于页面的字符集一样 -->
    <constant name="struts.i18n.encoding" value="gb2312"></constant>    
    <package name="general" extends="struts-default">

            <!-- 万能的action,可以匹配任何action -->
            <action name="*">
                <!-- 这样的处理可以访问WEB-INF下面的内容,一般来说,用户是不可以访问WEB-INF的内容,有利于安全 -->
                <!-- 这个思想也就是用户的一切请求都是action请求-->
                <result>/WEB-INF/content/{1}.jsp</result>
            </action>

    </package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值