在进行struts开发的过程中,总也是出现很多的乱码问题 ,但归根到底,也只是以下三种情况:

㈠页面显示中文乱码

㈡传递参数中文乱码

㈢国际化资源文件乱码

下面就这三中情况介绍怎么在具体项目中处理这些乱码问题。而对于整体的处理思想,是要统一编码为: UTF-8.(以myeclipse6支持的struts1.3为准)

㈠页面显示中文乱码

对于在页面中显示出现乱码,这个问题比较简单,便是检查你的JSP文件里是不是出现了中文要处理,因为JSP默认的编码格式为“ISO-8859-1”,当JSP中出现要处理的中文时,其显示就出现乱码了,这种情况一般出现在手写JSP,或修改时。因为在myeclipse6.0中,如果出现了编码错误时,程序不会让你保存,而是会提示你注意编码,这点很好。具体的修改办法是把

  1. <%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">
  1. <%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">

改成:

  1. <%.@ page language="java" import="java.util." pageEncoding="UTF-8">
  1. <%.@ page language="java" import="java.util." pageEncoding="UTF-8">

㈡传递参数中文乱码

传递参数出现的乱码,参数的内容为中文。比如在struts应用中,简单的一个登录界面中,需要传递的登录名为中文时,你没经处理之前,是会出现乱码传递的,为了让我们能看到显示的乱码,我们在对应的Action类的子类里,修改一下,用System.out把接受到的参数输出,代码如下:

  1. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  2. HttpServletResponse response) ...{
  3. DynaActionForm loginForm = (DynaActionForm) form;
  4. String username = (String) loginForm.get("username");
  5. String password = (String) loginForm.get("password");
  6. System.out.println("username:"+username);
  7. System.out.println("password:"+password);
  8. if (username.equals("ivorytower") && password.equals("123456")) ...{
  9. return mapping.findForward("success");
  10. }
  11. return mapping.findForward("fail");
  12. }
  1. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  2. HttpServletResponse response) ...{
  3. DynaActionForm loginForm = (DynaActionForm) form;
  4. String username = (String) loginForm.get("username");
  5. String password = (String) loginForm.get("password");
  6. System.out.println("username:"+username);
  7. System.out.println("password:"+password);
  8. if (username.equals("ivorytower") && password.equals("123456")) ...{
  9. return mapping.findForward("success");
  10. }
  11. return mapping.findForward("fail");
  12. }

那么当你提交了中文输入后就会出现乱码了。

具体的解决方法:

①修改Tomcat---->conf----->server.xml文件,在修改端口的标签后面加一行代码,如下:

  1. <Connector port="8080" protocol="HTTP/1.1"
  2. connectionTimeout="20000"
  3. redirectPort="8443"  URIEncoding="UTF-8"/>
  1. <Connector port="8080" protocol="HTTP/1.1"
  2. connectionTimeout="20000"
  3. redirectPort="8443"  URIEncoding="UTF-8"/>

②编写过滤器Filter

  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. public class CharacterEncodingFilter implements Filter ...{
  9. @Override
  10. public void destroy() ...{
  11. }
  12. @Override
  13. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException   {
  14. request.setCharacterEncoding("utf-8");
  15. chain.doFilter(request, response);
  16. }
  17. @Override
  18. public void init(FilterConfig arg0) throws ServletException ...{
  19. }
  20. }
  1. import java.io.IOException;
  2. import javax.servlet.Filter;
  3. import javax.servlet.FilterChain;
  4. import javax.servlet.FilterConfig;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.ServletRequest;
  7. import javax.servlet.ServletResponse;
  8. public class CharacterEncodingFilter implements Filter ...{
  9. @Override
  10. public void destroy() ...{
  11. }
  12. @Override
  13. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException   {
  14. request.setCharacterEncoding("utf-8");
  15. chain.doFilter(request, response);
  16. }
  17. @Override
  18. public void init(FilterConfig arg0) throws ServletException ...{
  19. }
  20. }

利用过滤器,把requst传递的中文参数都设成“UTF-8”编码。

③修改web.xml文件

打开项目里的web.xml文件,在前面加上如下代码:

  1. <filter>
  2. <filter-name>characterEncoding</filter-name>
  3. <filter-class>com.v512.example.CharacterEncodingFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>characterEncoding</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>
  1. <filter>
  2. <filter-name>characterEncoding</filter-name>
  3. <filter-class>com.v512.example.CharacterEncodingFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>characterEncoding</filter-name>
  7. <url-pattern>/*</url-pattern>
  8. </filter-mapping>

注意其过滤的URL为“/*”,表示当前的request请求。为了使设置生效,重起tomcat。

㈢国际化资源文件乱码

①利用JDK的native2ascii工具进行编码转换

国际化问题,主要是为了处理文件在浏览器上的显示问题,还是以登录界面来说,比如在中文浏览器上,我们要看到中文显示,对应在英文浏览器上要显示英文。那么我们在登录那个界面处理上,就不能直接写上我们的“用户名”“密码”等标识了。就要用标记转换输出了,修改为:

  1. <bean:message key="example.login.username"/>
  1. <bean:message key="example.login.username"/>

再者,打开项目下的资源配置文件ApplicationResources.properties,依据上面所写key值,设定成我们要的默认值(显示英文),比如

引用
      
#Resource for Parameter 'com.v512.example.struts.ApplicationResources 
#Project webexample2 
example.login.username=username 
example.login.password=password

现在我们动手新建一个资源文件,让其能显示中文,直接Ctrl+C,Ctrl+V。改名为ApplicationResources_zh.properties,代码如下:

引用
       
#Resource for Parameter 'com.v512.example.struts.ApplicationResources 
#Project webexample2 
example.login.username=用户名 
example.login.password=密码

但保存,myeclipse会报错,这时我们需要修改资源文件的编码格式。Windons---->Preferences---->Content Type------>Text----->JavaPropertiesFile,把其Default encoding改为“utf-8”,按“update”更新。这样就能进行保存了。但是当我们进行验证会不是成功时,仍然给我们的是乱码。

不急,我们还得做一项任务,打开DOS窗口,CMD到资源文件所在目录,运用JDK的native2ascii工具把我们新建的资源文件改成另一个名字的资源文件,例如bank.properties。命令如下:

引用
     
>native2ascii -encoding gbk ApplicationResources_zh.properties bank.properties 

打开bank.properties资源文件,自动生成的代码如下:

引用
      
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

example.login.username = \u7528\u6237\u540D 
example.login.password = \u5BC6\u7801

然后在myeclipse窗口中,把原来新建ApplicationResources_zh.properties 删除,并把bank.properties改为ApplicationResources_zh.properties (为了方便记忆,管理)。然后重起tomcat或进行reload文件,我们发现乱码问题没有了。

struts 乱码的更多相关文章

  1. struts乱码问题

    我觉得很有必要记录一下,在我搜便了网上乱码问题的解决方案无果之后几乎绝望. 一定要在提交的form 加上 methd =  post

  2. struts2.xml的配置与技巧

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  3. SSH 两个表全套增删改(运动员住宿管理)

    0.创建如下oracle的命令 create table HOTALINFO ( HOTALID ) not null, HOTALNAME ) not null, HOTALADDRESS ) no ...

  4. struts2_struts.xml配置文件讲解

    1.bean Bean详细讲解:https://www.cnblogs.com/lulu638/p/4340703.html 2.constant constant属性配置,可配置的属性可以参考def ...

  5. 2.SSH 两个表全套增删改(运动员住宿管理)

    0.创建如下oracle的命令 create table HOTALINFO ( HOTALID ) not null, HOTALNAME ) not null, HOTALADDRESS ) no ...

  6. java web 学习十(HttpServletRequest对象1)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  7. struts jsp传值到action,乱码的解决方案

    使用了Struts框架,前台写好了编码为utf-8 <%@ page language="java" contentType="text/html; charset ...

  8. Struts 2中如何解决中文乱码问题?

    在配置文件中定义如下常量值配置: <constant name=”struts.i18n.encoding” value=”UTF-8”/> 注意:只能解决post请求的乱码问题,针对ge ...

  9. 使用Struts,前台提交给后台的汉字为乱码

    首先产生乱码的根本原因为前台JSP页面和后台Action的编码方式不同! Struts的默认编码方式为UTF-8 JSP的默认编码方式为GB18030 解决方案: 前后台编码方式一致: 结合拦截器的使 ...

随机推荐

  1. 转 用好HugePage,告别Linux性能故障

    超过32G 的数据库,可以是使用如下方法配置. ######### Slow Performance with High CPU Usage on 64-bit Linux with Large SG ...

  2. 错误之Illegal mix of collations for operation 'like'

    内容来自博客:https://www.cnblogs.com/install/p/4417527.html MySQL Illegal mix of collations for operation ...

  3. CentOS 7.x升级内核

    第一种针对当前内核版本的小版本升级可以采用如下方法: [root@localhost ~]# uname -r -.el7 [root@localhost ~]# yum list kernel [r ...

  4. Linux--NiaoGe-Service-06

    Linux网络排错 思路: 硬件问题: 首先排除硬件故障,包括网线.Hub.Switch.Router.网卡.设备配置规则等等. 软件问题: 1.网卡的IP/Netmask设置错误 IP.Netmas ...

  5. 通过Chrome执行watir-webdriver

    1.http://code.google.com/p/chromedriver/downloads/list  下载chromedriver驱动文件chromedriver.exe 2.把驱动文件放在 ...

  6. VC和MATLAB混合开发需要注意的一个问题

    作者:朱金灿 来源:http://blog.csdn.net/clever101 如果你的操作系统是64位操作系统,那么直接运行MATLAB的安装文件下的Setup.exe会默认安装的是64位的MAT ...

  7. 更改IDEA默认使用JDK1.5编译项目

    在使用IDEA编译项目时,总是提示我编译失败,有些功能是在1.5之后的版本才支持.废了好大的功夫才编译成功.下面呢,就是更改编译的过程了! 一.初步更改 使用快捷键Ctrl+Alt+Shift+S打开 ...

  8. JavaScript_1_简介

    1. JavaScript属于客户端脚本语言 2. JavaScript用来改进网页设计.验证表单.检测浏览器.创建cookies,以及更多的应用 a. 是为HTML设计者提供的一种编程工具 b. 可 ...

  9. 洛谷 P2419 [USACO08JAN]牛大赛Cow Contest

    题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a p ...

  10. 拼凑json的实例

    一,拼凑  json 串 并传到后台 function writeToJson(num){ var i = 1; var jsonData = "["; for(i=1;i< ...