struts国际化


1、对JSP文件的文本,图片和按钮进行国际化

 1).设置字符编码,将JSP页面字符编码统一设为“UTF-8”

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 
 2).对文本国际化
 即页面要显示内容:<bean:message>标签从Resource Bundle中获得。
 <bean:message key="" />
 
 3).对按钮国际化

 常规的定义按钮为<html:submit property="submit" value="Submit" /><html:reset />

 修改为:<html:submit property="submit" >
   <bean:message key="jsp.page.submit" />
  </html:submit>
  <html:reset property="reset">
   <bean:message key="jsp.page.reset" />
  </html:reset>

 4).对图片国际化
 常规:<html:img page="/image/a.gif" alt="a" />
 
 改为:<html:img pageKey="page.image.a" altKey="page.a" />
 
 <html:img>标签将从Resource Bundle中读取图片名,当移到图片上显示的替代文本内容。其中pageKey属性指定图片名,altKey属性指定当用户将鼠标移到图片上显示的替代文本内容。

2、创建临时中文资源文件。

3、对临时中文资源文件进行转码。

 在JDK中提供了native2ascii命令。在DOS下执行以下命令,将生成指定编码的资源文件。

 native2ascii -encoding gb2312 application_temp.properties application_zh_CN_properties

 application_temp.properties为刚创建的临时文件。
 application_zh_CN.properties为生成的文件名。

4、创建其它语言环境的资源文件

5、采用servlet过滤器设置请求数据的字符编码

 /**
 *
 */
package com.yzinfo.gzzgov.tools;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author yuxueming
 *
 */
public class StrutsFiter implements  javax.servlet.Filter {

    protected String encoding = null;


    protected FilterConfig filterConfig = null;


    protected boolean ignore = true;


    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }

 

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
        throws IOException, ServletException {


        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

        // Pass control on to the next filter
        chain.doFilter(request, response);

    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");//取web.xml中配的参数
        String value = filterConfig.getInitParameter("ignore");
        if (value == null)
            this.ignore = true;
        else if (value.equalsIgnoreCase("true"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

}

WEB.XML中的配置
 <filter>
  <filter-name>SetEncoding</filter-name>
  <filter-class>com.yzinfo.gzzgov.tools.StrutsFiter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>gb2312</param-value>
  </init-param>
 </filter>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值