1. 程式人生 > >Struts2亂碼問題 action1跳轉action2之後引數為亂碼

Struts2亂碼問題 action1跳轉action2之後引數為亂碼

使用Struts2,在專案中配置:
       1、在jsp頁面上設定:

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<!-- 登入表單 -->

<form action="login/login_show.action" method="post">
<div><b>使用者名稱 :</b> <input type="text"/ name="username" maxlength="10" title="輸入使用者名稱"/></div>
<div><b>密碼 : </b><input type="password" name="password" maxlength="8" title="輸入密碼"/></div>
<div><input type="submit" value="登 錄"/></div>
</form>

       頁面編碼和jsp程式碼都設定為UTF-8。

       2、struts.xml中,設定:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.locale" value="zh_CN"></constant>
 
<package name="login" extends="struts-default" namespace="/login">
<action name="login_*" class="indi.smt.action.LoginAction" method="{1}">
<result name="show" type="redirectAction">
<param name="username">${username}</param>
<param name="password">${password}</param>
<param name="result">${result}</param>
<param name="un">${un}</param>
<param name="psw">${psw}</param>
<param name="actionName">login_detail</param>
</result>
<result name="detail">/WEB-INF/jsp/show.jsp</result>
<result name="main">/WEB-INF/jsp/login.jsp</result>
</action>
</package>

<package name="result" extends="struts-default" namespace="/result">
<action name="login_*" class="indi.smt.action.ResultAction" method="{1}">
<result name="success">/WEB-INF/jsp/success.jsp</result>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</action>
</package>
</struts>
       均設定為UTF-8。

       3、並配置一個字元過濾器:
package indi.smt.filter;

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 EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("開始編碼轉換");
req.setCharacterEncoding("utf-8");
res.setCharacterEncoding("utf-8");
filterChain.doFilter(req, res);
System.out.println("完成編碼轉換");
}
public void init(FilterConfig arg0) throws ServletException {
}
}
       將requert、response均設定為UTF-8。

      4、web.xml中配置(3)中的過濾器:

  
	<filter>
<filter-name>encoding</filter-name>
<filter-class>indi.smt.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

      5、回顯jsp頁面:

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="http://mayh0991.blog.163.com/<%=basePath%>">
<title>結果顯示</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>輸入結果</h2><br>
<div>
<table>
<tbody>
<tr>
<td><%=username %></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

以上只列出這個亂碼的字元回顯。

==========================================================================================
提交表單之後,在IED控制檯列印的表單送到後臺的引數username中文為正常字元(無亂碼)。但是回顯到/login/login_show.action的show.jsp中的username為亂碼(如5所示)。

現在看到一個部落格寫的應該是解決方案,但是沒有搞明白是怎麼回事:
struts2 action 跳action 傳遞中文亂碼問題解決