Struts中文(包括资源文件)问题解决方案

类别:Java 点击:0 评论:0 推荐:
俺才学习STRUTS碰到中文问题,研究了一整天,在网上狂搜一通,整理一下成果给大家分享!不足处大家能指正,谢谢! STRUTS资源文件中文字符集问题

native2ascii -encoding GBK F:\myStruts\src\mystruts\ApplicationResources.properties F:\myStruts\src\mystruts\ApplicationResources_zh_CN.properties将原有的资源文件转换成GBK码。并在WEB.XML文件中加入以下代码(红色部分):

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>application</param-name>

      <param-value>mystruts.ApplicationResources_zh_CN</param-value>

    </init-param>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <filter>

    <filter-name>Set Character Encoding</filter-name>

    <filter-class>filters.SetCharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>GBK</param-value>

    </init-param>

    <init-param>

      <param-name>ignore</param-name>

      <param-value>true</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>Set Character Encoding</filter-name>

    <url-pattern>*.do</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>Set Character Encoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter>

    <filter-name>Request Dumper Filter</filter-name>

    <filter-class>filters.RequestDumperFilter</filter-class>

  </filter>

  <!--<filter-mapping>

    <filter-name>Request Dumper Filter</filter-name>

    <url-pattern>*.do</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>Request Dumper Filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>-->

  <taglib>

    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>

  </taglib>

</web-app>

STRUTS中文问题 解决办法1:(未实现)

jakarta-struts-1.0.2-src\src\share\org\apache\struts\util\PropertyMessageResources.java

将第289行的:messages.put(messageKey(localeKey, key),props.getProperty(key));

改成下面的代码:

String sProps = props.getProperty(key);

try

{

sProps = new String(sProps.getBytes("ISO-8859-1"),"GBK");

}

catch (Exception e)

{

e.printStackTrace();

}

messages.put(messageKey(localeKey, key),sProps);

编译后替换原有STRUTS.JAR中的PropertyMessageResources类。

解决办法2:

在工程中加入2个类filters.RequestDumperFilter、filters.SetCharacterEncodingFilter(C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\jsp-examples\WEB-INF\classes\filters),修改WEB.XML代码(红色部分)如下:

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>application</param-name>

      <param-value>mystruts.ApplicationResources_zh_CN</param-value>

    </init-param>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <filter>

    <filter-name>Set Character Encoding</filter-name>

    <filter-class>filters.SetCharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>GBK</param-value>

    </init-param>

    <init-param>

      <param-name>ignore</param-name>

      <param-value>true</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>Set Character Encoding</filter-name>

    <url-pattern>*.do,/*</url-pattern>

  </filter-mapping>

  <taglib>

    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>

  </taglib>

</web-app>

解决办法3:

对于处理 Struts Framework 中文编码问题有多种方式,在此我们尝试继承 ActionServlet 类并覆盖 process 方法,于子类process 方法中设定请求编码。由此展现ActionServlet 扮演前端Controller 之特质。

首先,点击选择 JBuilder 主菜单 File | New Classes 准备产生一新的类。并继承自org.apache.struts.action.ActionServlet

并将预设的类名称由Untitled1 改为GBKActionServlet。按下 OK 按钮后即产生程序代码。点击选择 JBuilder 主菜单Wizards | Override Methods… 激活覆盖方法精灵,覆盖方法精灵会自动列出父类 ActionServlet 所有可供覆盖之方法。您可于 Available methods 清单方块中选取process (HttpServletRequest request, HttpServletResponseresponse) 方法,完成后按下 OK 按钮。

此时我们可修改覆盖后 process 方法程序代码如下:request.setCharacterEncoding ("GBK") ;super.process (request, response) ;

首行程序代码将 process 传入之参数 request 设定为 GBK编码,其余行为同父类。接下来我们将要修改 web.xml部署描述档,以指定 Servlet 之进入点,首先点击选择左上方项目窗格中的 web.xml。此时,左下方结构窗格将出现 web.xml 内 XML 标记之树状结构。您可以点击选择 Servlets 内的 Action 项目。

您可以按下 Servlet class 项目右方 [ … ] 按钮选取新的进入点替换预设之ActionServlet,也就是之我们撰写之util. GBKActionServlet。倘若您非常熟悉 web.xml 结构,同样地可以直接点击选择内容窗格内的 Sources 页次,直接以文字编辑器修改 <servlet-class> 标记的内容。web.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <filter>

    <filter-name>Request Dumper Filter</filter-name>

    <filter-class>filters.RequestDumperFilter</filter-class>

  </filter>

  <filter>

    <filter-name>Set Character Encoding</filter-name>

    <filter-class>filters.SetCharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>GBK</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>Set Character Encoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter-mapping>

    <filter-name>Request Dumper Filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>util. GBKActionServlet</servlet-class>

    <init-param>

      <param-name>application</param-name>

      <param-value>mystruts.ApplicationResources_zh_CN</param-value>

    </init-param>

    <init-param>

      <param-name>config</param-name>

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  <taglib>

    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

  </taglib>

  <taglib>

    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>

    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>

  </taglib>

</web-app>

本文地址:http://com.8s8s.com/it/it16753.htm