struts2 中文乱码解决办法

本文介绍了如何在Struts2应用中解决中文乱码问题,包括创建一个用于转换编码的Filter,修改web.xml配置,设置JSP页面编码,以及在struts.xml中修改默认编码设定。同时,提到了数据库连接字符串中指定字符编码的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 适合情况 -> 从jsp传入到action时的乱码情况,这里以GBK为例

 

1.建立一个用于转换编码的filter

文件位置举例:src.util.SetCharacterEncodingFilter.java

[java]  view plain copy
  1. package util;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.Filter;  
  5. import javax.servlet.FilterChain;  
  6. import javax.servlet.FilterConfig;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10. import javax.servlet.UnavailableException;  
  11.   
  12. /** 
  13.  * Example filter that sets the character encoding to be used in parsing the 
  14.  * incoming request 
  15.  */  
  16. public class SetCharacterEncodingFilter implements Filter {  
  17.   
  18.     /** 
  19.      * Take this filter out of service. 
  20.      */  
  21.     public void destroy() {  
  22.     }  
  23.     /** 
  24.      * Select and set (if specified) the character encoding to be used to 
  25.      * interpret request parameters for this request. 
  26.      */  
  27.     public void doFilter(ServletRequest request, ServletResponse response,  
  28.     FilterChain chain)throws IOException, ServletException {  
  29.   
  30.     request.setCharacterEncoding("gbk");  
  31.   
  32.     // 传递控制到下一个过滤器  
  33.     chain.doFilter(request, response);  
  34.     }  
  35.   
  36.     public void init(FilterConfig filterConfig) throws ServletException {  
  37.     }  
  38. }  

 

2.修改web.xml,在struts的FilterDispatcher映射之前添加2个filter

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     <listener>  
  8.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  9.     </listener>  
  10.     <filter>  
  11.         <filter-name>Set Character Encoding</filter-name>   
  12.         <filter-class>util.SetCharacterEncodingFilter</filter-class>   
  13.     </filter>   
  14.     <filter-mapping>   
  15.         <filter-name>Set Character Encoding</filter-name>  
  16.         <url-pattern>/*</url-pattern>   
  17.     </filter-mapping>  
  18.       
  19.     <filter>   
  20.         <filter-name>struts-cleanup</filter-name>   
  21.         <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>   
  22.     </filter>  
  23.     <filter-mapping>  
  24.         <filter-name>struts-cleanup</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.       
  28.       
  29.       
  30.       
  31.     <filter>  
  32.         <filter-name>struts2</filter-name>  
  33.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  34.     </filter>  
  35.     <filter-mapping>  
  36.         <filter-name>struts2</filter-name>  
  37.         <url-pattern>/*</url-pattern>  
  38.     </filter-mapping>  
  39. </web-app>  

 

3.你的JSP头应该有

<%@ page language="java" pageEncoding="GBK"%>

 

4.在struts.xml中修改默认的编码设定

[xhtml]  view plain copy
  1. <struts>  
  2.     <constant name="struts.i18n.encoding" value="gbk"></constant>  
  3.   
  4. ...  
  5. ...  
  6. ...  
  7.   
  8. </struts>  

 

基本上就这样可以解决大多传入的字符乱码问题

 

PS:如果是数据库提取字符乱码,比如mysql,确认你的数据库内字符是gbk,并且连接字符串指定了字符编码

<property name="url" value="jdbc:mysql://localhost/database?useUnicode=true&amp;characterEncoding=gbk"></property>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值