﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-有琴逍遥-文章分类-struts</title><link>http://www.blogjava.net/daiyinchuan/category/16845.html</link><description>Nothing is particularly hard if you divide it into small jobs.</description><language>zh-cn</language><lastBuildDate>Thu, 01 Mar 2007 01:20:35 GMT</lastBuildDate><pubDate>Thu, 01 Mar 2007 01:20:35 GMT</pubDate><ttl>60</ttl><item><title>[转帖]"struts中文问题"，"struts国际化问题"终极解决方案</title><link>http://www.blogjava.net/daiyinchuan/articles/78742.html</link><dc:creator>有琴逍遥</dc:creator><author>有琴逍遥</author><pubDate>Thu, 02 Nov 2006 10:13:00 GMT</pubDate><guid>http://www.blogjava.net/daiyinchuan/articles/78742.html</guid><wfw:comment>http://www.blogjava.net/daiyinchuan/comments/78742.html</wfw:comment><comments>http://www.blogjava.net/daiyinchuan/articles/78742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/daiyinchuan/comments/commentRss/78742.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/daiyinchuan/services/trackbacks/78742.html</trackback:ping><description><![CDATA[
		<p>实现Struts的国际化，其实一切并不复杂，下面是具体步骤： </p>
		<p>0.遇到的问题(这些问题也许不会同时出现) <br />a.中文数据从数据库中到jsp中后就变成了"????" <br />b.做好的中文properties文件，其中的中文value在页面显示乱码 <br />c.jsp文件中的中文到浏览器后显示时也是乱码（建议不要在jsp文件中输入中文，尽量放在properties文件中） <br />d.由jsp传给bean的中文值，再由bean传回页面又是乱码 <br />e.当更换本地浏览器的语言选项时，Web应用程序不能自动根据你的locale选择合适的*.properties文件。导致Web应用程序不能国际化。 </p>
		<p>1.环境： <br />Web服务器： Tomcat 5.0.19 <br />操作系统： Win2000 Server <br />JVM ： jdk 1.4 <br />数 据 库： Oracle 8.1.7 <br />开发工具： struts studio 5.2 pro for eclipse </p>
		<p>2.先将所有*.jsp 网页中开头处加入 <br />&lt;%@ page language="java" contentType="text/html; charset=utf-8" %&gt; <br />再设置&lt;html:html locale = "true"&gt; </p>
		<p>3.然后编辑好两个*.properties文件，放在classes文件夹下你指定的地方，这里是放在/web-inf/classes/com/wiley 下，它们分别是： <br />ApplicationResources.properties (英文资源文件) <br />ApplicationResources_zh.properties (中文资源文件) <br />随便用什么工具编写都行啊！ </p>
		<p>4.将ApplicationResources_zh.properties转码成gb2312。上面引文说要转成UTF-8，结果我试了，不行。转成gb2312就行了，操作是。 <br />将ApplicationResources_zh.properties更名为ApplicationResources_xx.properties <br />在DOS命令行进入ApplicationResources_xx.properties所在的文件夹 <br />使用命令：native2ascii -encoding gb2312 ApplicationResources_xx.properties </p>
		<p>ApplicationResources_zh.properties(至于你为什么会出现“native2ascii不是内部命令”，请查其它资料，可能你要设置环境变量，因为他是jdk的文件夹bin下的一个应用程序) </p>
		<p>5.接下来配置struts-config.xml,很简单，我们加入： <br />&lt;message-resources parameter="com.wiley.ApplicationResources"/&gt; 就行了； </p>
		<p>到此已能解决大多数中文问题。如上面所说的a,b,e 现在打开浏览器，选择菜单：工具》internet选项》</p>
		<p>语言，将“中文－中国[zh-cn]”删掉，添加一个“英语－英国[zh-gb]”确定后，重启Tomcat,输入网址你就会发现，你的页面的文本信息就会用的是ApplicationResources.properties (英文资源文件)中的内容。如果换回“中文－中国[zh-cn]”，它就会显示ApplicationResources_zh.properties (中文资源文件)中的中文内容。 </p>
		<p>至于问题“c.jsp文件中的中文到浏览器后显示时也是乱码” 你就要用与第4步类似的方法来重新对*.jsp 文件编码，这时-encoding的参数就要用UTF-8了，如果你用的也是struts studio 5.2 pro for eclipse工具，这一步就免了。它会自动用UTF-8的格式存储。 <br />至于问题“d.由jsp传给bean的中文值，再由bean传回页面又是乱码”的解决，我只是加了个过滤器。 <br />你可以现在web.xml中加入： </p>
		<p>&lt;filter&gt; <br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt; <br />&lt;filter-class&gt;com.wiley.SetCharacterEncodingFilter&lt;/filter-class&gt; <br />&lt;init-param&gt; <br />&lt;param-name&gt;encoding&lt;/param-name&gt; <br />&lt;param-value&gt;utf-8&lt;/param-value&gt; <br />&lt;/init-param&gt; <br />&lt;init-param&gt; <br />&lt;param-name&gt;ignore&lt;/param-name&gt; <br />&lt;param-value&gt;true&lt;/param-value&gt; <br />&lt;/init-param&gt; <br />&lt;/filter&gt; <br />&lt;filter-mapping&gt; <br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt; <br />&lt;servlet-name&gt;action&lt;/servlet-name&gt; <br />&lt;/filter-mapping&gt; </p>
		<p>然后在你指定的包内加个java文件 我放在了/web-inf/classes/com/wiley 里，下面是源代码： <br />/* <br />* XP Forum <br />* <br />* Copyright (c) 2002-2003 RedSoft Group. All rights reserved. <br />* <br />*/ <br />package com.huahang.tj.struts.filters; </p>
		<p>import javax.servlet.*; <br />import java.io.IOException; </p>
		<p>/** <br />* &lt;p&gt;Filter that sets the character encoding to be used in parsing the <br />* incoming request, either unconditionally or only if the client did not <br />* specify a character encoding. Configuration of this filter is based on <br />* the following initialization parameters:&lt;/p&gt; <br />* &lt;ul&gt; <br />* &lt;li&gt;&lt;strong&gt;encoding&lt;/strong&gt; - The character encoding to be configured <br />* for this request, either conditionally or unconditionally based on <br />* the &lt;code&gt;ignore&lt;/code&gt; initialization parameter. This parameter <br />* is required, so there is no default.&lt;/li&gt; <br />* &lt;li&gt;&lt;strong&gt;ignore&lt;/strong&gt; - If set to "true", any character encoding <br />* specified by the client is ignored, and the value returned by the <br />* &lt;code&gt;selectEncoding()&lt;/code&gt; method is set. If set to "false, <br />* &lt;code&gt;selectEncoding()&lt;/code&gt; is called &lt;strong&gt;only&lt;/strong&gt; if the <br />* client has not already specified an encoding. By default, this <br />* parameter is set to "true".&lt;/li&gt; <br />* &lt;/ul&gt; <br />* <br />* &lt;p&gt;Although this filter can be used unchanged, it is also easy to <br />* subclass it and make the &lt;code&gt;selectEncoding()&lt;/code&gt; method more <br />* intelligent about what encoding to choose, based on characteristics of <br />* the incoming request (such as the values of the &lt;code&gt;Accept-Language&lt;/code&gt; <br />* and &lt;code&gt;User-Agent&lt;/code&gt; headers, or a value stashed in the current <br />* user′s session.&lt;/p&gt; <br />* <br />* @author &lt;a href="<a href="mailto:jwtronics@yahoo.com&quot;&gt;John">mailto:jwtronics@yahoo.com"&gt;John</a> Wong&lt;/a&gt; <br />* <br />* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $ <br />*/ <br />public class SetCharacterEncodingFilter implements Filter { </p>
		<p>// ----------------------------------------------------- Instance Variables </p>
		<p>
				<br />/** <br />* The default character encoding to set for requests that pass through <br />* this filter. <br />*/ <br />protected String encoding = null; </p>
		<p>
				<br />/** <br />* The filter configuration object we are associated with. If this value <br />* is null, this filter instance is not currently configured. <br />*/ <br />protected FilterConfig filterConfig = null; </p>
		<p>
				<br />/** <br />* Should a character encoding specified by the client be ignored? <br />*/ <br />protected boolean ignore = true; </p>
		<p>
				<br />// --------------------------------------------------------- Public Methods </p>
		<p>
				<br />/** <br />* Take this filter out of service. <br />*/ <br />public void destroy() { </p>
		<p>this.encoding = null; <br />this.filterConfig = null; </p>
		<p>} </p>
		<p>
				<br />/** <br />* Select and set (if specified) the character encoding to be used to <br />* interpret request parameters for this request. <br />* <br />* @param request The servlet request we are processing <br />* @param result The servlet response we are creating <br />* @param chain The filter chain we are processing <br />* <br />* @exception IOException if an input/output error occurs <br />* @exception ServletException if a servlet error occurs <br />*/ <br />public void doFilter(ServletRequest request, ServletResponse response, <br />FilterChain chain) <br />throws IOException, ServletException { </p>
		<p>// Conditionally select and set the character encoding to be used <br />if (ignore || (request.getCharacterEncoding() == null)) { <br />String encoding = selectEncoding(request); <br />if (encoding != null) <br />request.setCharacterEncoding(encoding); <br />} </p>
		<p>// Pass control on to the next filter <br />chain.doFilter(request, response); </p>
		<p>} </p>
		<p>
				<br />/** <br />* Place this filter into service. <br />* <br />* @param filterConfig The filter configuration object <br />*/ <br />public void init(FilterConfig filterConfig) throws ServletException { </p>
		<p>this.filterConfig = filterConfig; <br />this.encoding = filterConfig.getInitParameter("encoding"); <br />String value = filterConfig.getInitParameter("ignore"); <br />if (value == null) <br />this.ignore = true; <br />else if (value.equalsIgnoreCase("true")) <br />this.ignore = true; <br />else if (value.equalsIgnoreCase("yes")) <br />this.ignore = true; <br />else <br />this.ignore = false; </p>
		<p>} </p>
		<p>
				<br />// ------------------------------------------------------ Protected Methods </p>
		<p>
				<br />/** <br />* Select an appropriate character encoding to be used, based on the <br />* characteristics of the current request and/or filter initialization <br />* parameters. If no character encoding should be set, return <br />* &lt;code&gt;null&lt;/code&gt;. <br />* &lt;p&gt; <br />* The default implementation unconditionally returns the value configured <br />* by the &lt;strong&gt;encoding&lt;/strong&gt; initialization parameter for this <br />* filter. <br />* <br />* @param request The servlet request we are processing <br />*/ <br />protected String selectEncoding(ServletRequest request) { </p>
		<p>return (this.encoding); </p>
		<p>} </p>
		<p>}//EOC <br />到此我遇到的中文问题已全部得到解决，并从中理解到struts的国际化的深刻含义。 <br />我个人觉得struts作为一个功能强大的应用框架，应该早就考虑到它的国际化问题，并在实际应用中不会很复杂，只要我们遵循一些规则，就可以尽情享受struts给我们带来的无穷乐趣。希望以上所述对大家有所帮助。   </p>
<img src ="http://www.blogjava.net/daiyinchuan/aggbug/78742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/daiyinchuan/" target="_blank">有琴逍遥</a> 2006-11-02 18:13 <a href="http://www.blogjava.net/daiyinchuan/articles/78742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>