Struts2中文乱码的解决

 

一、可以使用spring提供的编码过滤器,在web.xml中进行简单配置即可:

 

 

    <!-- 配置编码,解决中文乱码 -->    

    <filter>

        <filter-name>encodingFilter</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>

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

            <param-value>UTF-8</param-value>

        </init-param>

    </filter>

    <filter-mapping>

        <filter-name>encodingFilter</filter-name>

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

    </filter-mapping>

    <filter>

        <filter-name>actionFilter</filter-name>

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

        <init-param>

            <param-name>actionPackages</param-name>

            <param-value>com.yun.facePot</param-value>

        </init-param> 

    </filter>

    <filter-mapping>

        <filter-name>actionFilter</filter-name>

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

    </filter-mapping>

 

 

 

 

我们只需在web.xml中配置

org.springframework.web.filter.CharacterEncodingFilter

这个过滤器,然后设置过滤规则,这样符合这个请求路径的编码都会被这个过滤器过滤。

 

 

二、自定义过滤器实现编码过滤:

 

 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%@ page contentType="text/html; charset=UTF-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
	<base href="<%=basePath%>">    
	<title>My JSP 'index.jsp' starting page</title>  
	<meta http-equiv="pragma" content="no-cache">  
	<meta http-equiv="cache-control" content="no-cache">  
	<meta http-equiv="expires" content="0">     
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
	<meta http-equiv="description" content="This is my page">  
  </head>   
  <body>  
  <form action="test1" method="post">  
	用户名:<input type="text" name="username"/><br/>  
	密 码:<input type="password" name="password">  
  <input type="submit" value="提交">  
  </form>  
  </body>  
</html>  

  

success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
	<base href="<%=basePath%>">     
	<title>My JSP 'success.jsp' starting page</title>     
	<meta http-equiv="pragma" content="no-cache">  
	<meta http-equiv="cache-control" content="no-cache">  
	<meta http-equiv="expires" content="0">     
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
	<meta http-equiv="description" content="This is my page">  
  </head>  
  <body>  
	登录成功!欢迎:${username}  
  </body>  
</html>  

 

error.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
	<base href="<%=basePath%>">     
	<title>My JSP 'error.jsp' starting page</title>     
	<meta http-equiv="pragma" content="no-cache">  
	<meta http-equiv="cache-control" content="no-cache">  
	<meta http-equiv="expires" content="0">     
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
	<meta http-equiv="description" content="This is my page">  
  </head>   
  <body>  
	登录失败!  
	<a href="index.jsp">返回重新登录</a>  
  </body>  
</html>  

 

 LoginAction.java:

package com.cz.action;  
import java.io.UnsupportedEncodingException;  
import javax.servlet.http.HttpServletRequest;  
import org.apache.struts2.ServletActionContext;  
import com.opensymphony.xwork2.ActionContext;  
import com.opensymphony.xwork2.ActionSupport;  
public class LoginAction extends ActionSupport {  
	private String username;  
	private String password;  
	public String getUsername() {  
		return username;  
	}  
	public void setUsername(String username) {  
		this.username = username;  
	}  
	public String getPassword() {  
		return password;  
	}  
	public void setPassword(String password) {  
		this.password = password;  
	}   
	public String execute(){  
		System.out.println("到达execute。。。。");  
		return null;         
	}     
	public String login(){           
		System.out.println("到达login。。。。用户名:"+username);  
		if("啊".equals(username) && "123".equals(password)){  
			return "success";  
		}  
		return "login";  
	}  
}  

 

过滤器FilterEncoding.java

import java.io.IOException;  
import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
public class FilterEncoding implements Filter {  
	protected String encoding; // 接收字符编码  
	protected FilterConfig filterConfig; // 初始化配置  
	public void init(FilterConfig filterConfig) throws ServletException {  
		// 从web.xml文件中读取encoding的值  
		encoding = filterConfig.getInitParameter("encoding");  
	}  
	// doFilter方法  
	public void doFilter(ServletRequest request, ServletResponse response,  
			FilterChain chain) throws IOException, ServletException {  
		request.setCharacterEncoding(encoding);  
		chain.doFilter(request, response);  
	}  
	public void destroy() {  
  
	}  
}  

 

 web.xml:

 

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"  
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-app_2_5.xsd">  
<welcome-file-list>  
<welcome-file>index.jsp</welcome-file>  
</welcome-file-list>  
<filter>  
<filter-name>struts2</filter-name>  
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>         
</filter>  
<filter>   
 <filter-name>encoding</filter-name>   
	 <filter-class>FilterEncoding  </filter-class>   
 <init-param>   
	  <param-name>encoding</param-name>   
	  <param-value>utf-8</param-value>   
 </init-param>   
</filter>     
<filter-mapping>  
 <filter-name>encoding</filter-name>  
 <url-pattern>/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
<filter-name>struts2</filter-name>  
<url-pattern>/*</url-pattern>  
</filter-mapping>  
</web-app>  

  

struts.xml:

 

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
"http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>  
<package name="default" namespace="/" extends="struts-default">        
	<action name="test1" class="com.cz.action.LoginAction" method="login">  
		<result name="success">/success.jsp</result>  
		<result name="login">/error.jsp</result>  
	</action>  
</package>  
</struts>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值