JSP/JSF的国际化(字符集)使用心得

这段时间自学了一下一些主流的web框架,期间涉及到JSP/JSF的的国际化(字符集)问题,让我很是头疼,所以现在把一些使用心得总结一下,希望让后来人少走一些弯路,如有不对的地方,也请大家指正。

 

首先讲一下JSP的国际化(字符集)问题:

一. JSP文件中hardcode的非英语字符

   1.1 当然先要把JSP文件格式保存成UTF-8

   1.2 JSP头部声明,如下:

 

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

      Html的meta声明,如下: 

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

二. request和response中的非英语字符

    2.1 使用Filter,代码如下:  

public class EncodingFilter implements Filter {
	
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		chain.doFilter(req, resp);
	}

	public void init(FilterConfig cfg) throws ServletException {

	}
	
	public void destroy() {

	}
}

    2.2 request参数的取得(Tomcat 6.0.14)

String param = new String(req.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

 

    也可以修改Tomcat的配置文件server.xml中,形如:  

<Connector ... URIEncoding="UTF-8"/>
 

三. JSP页面国际化

    3.1 编写properties文件,使用JInto(Eclipse插件),使用方法见截图

          也可以手写properties文件,再用native2ascii 转换

          先写三个properties文件,用语言和国家代号区分

            sample_zh_CN.properties

            sample_en_US.properties

            sample_ja_JP.properties

           在properties文件中直接写相应的语言,中文的写中文,英文的写英文,日文的写日文

           写完后再用native2ascii 转换

             native2ascii -encoding UTF-8 sample_zh_CN.properties sample_zh_CN_result.properties

 

    3.2 使用JSTL在JSP上显示多国语   

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!-- set locale -->
<fmt:setLocale value="zh_CN"/>
<!-- global(default) resource bundle-->
<fmt:setBundle basename="view"/>
<!-- get resource by key reference to global(default) resource bundle-->
<fmt:message key="login"/> 

 

最后讲一下JSF中的国际化(字符集)问题:

    首先,JSP的处理方案可以完全照搬到JSF上,一字不改,一样可以运行,这里只要讲述使用JSF的backing bean(managed-bean)设置页面Locale

 

    一. JSF的faces-config.xml设置resource bundle

<faces-config version="1.2" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

    <application>
        <resource-bundle>
            <base-name>view</base-name>
            <var>scr</var>
        </resource-bundle>        
    </application>

       也可以直接在JSF文件中写  

<f:loadBundle basename="view" var="src"/>

   

    二. 在JSF文件中设置locale   

<f:view locale="zh_CN">

    这样JSF页面也能正常显示非英文字符。

 

    下面问题来了,页面多国语切换!

    JSP(JSTL)中很容易,如下 

<fmt:setLocale value="${locale}"/>

    只要在session中存一个locale变量,随时改变,页面上的<fmt:message>标签就会自动变化

 

    但是,JSF中使用managed-bean就完全不行,不是换不了,就是乱换

    代码如下,要是哪位有解决方案,请指教   

<faces-config version="1.2" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

    <application>
        <resource-bundle>
            <base-name>view</base-name>
            <var>scr</var>
        </resource-bundle>        
    </application>
    
    <managed-bean>
        <managed-bean-name>localBean</managed-bean-name>
        <managed-bean-class>LocalBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>locale</property-name>
            <value>en_US</value>
        </managed-property>
    </managed-bean>

</faces-config>
   
public class LocalBean {
    
    private String locale;

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }
    
    public void setLang() {       
        
    }

}
   
<f:view locale="#{localBean.locale}">
    <h:form>
        <h:selectOneRadio value="#{localBean.locale}">
            <f:selectItem itemLabel="CHN" itemValue="zh_CN"/>
            <f:selectItem itemLabel="ENG" itemValue="en_US"/>
            <f:selectItem itemLabel="JPN" itemValue="ja_JP"/>
        </h:selectOneRadio>
        <h:commandButton value="action" action="#{localBean.setLang}"/>

    </h:form>    
    <h1><h:outputText value="#{scr.login}" /></h1>
</f:view>
    

    最后,我在网上看到一篇关于字符集问题文章,写的不错,大家有空可以看看。

    http://china.manufacturer.com/article/study_for_character_encoding_java.htm     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值