struts2.0中个get请求乱码,使用过滤器仍然乱码,各位高手帮帮了
监听类EncodeFilter.java
package com.qian.tools;import java.io.IOException;import java.io.UnsupportedEncodingException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import org.apache.struts2.dispatcher.FilterDispatcher;import org.omg.CORBA.Request;import com.qian.mian.MainListener;public class EncodeFilter implements Filter{ private String edcoding=null; private boolean ignore=true; public void destroy() { edcoding=null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { if(ignore==true||request.getCharacterEncoding()==null){ String encoding=setCharacterEncoding(request); System.out.println("请求"+request.getParameter("sms.username")); if(encoding!=null){ //request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); System.out.println("en:"+encoding); } } System.out.println( request.getAttribute("username")); filterChain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.edcoding=filterConfig.getInitParameter("encoding"); String value=filterConfig.getInitParameter("ignore"); System.out.println(value); if(value==null){ this.ignore=true; }else if(value.equalsIgnoreCase("true")){ this.ignore=true; }else{ this.ignore=false; } } public String setCharacterEncoding(ServletRequest request){ return this.edcoding; }}
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>encode</filter-name> <filter-class>com.qian.tools.EncodeFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config></web-app>
<?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> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="struts2" namespace="/" extends="struts-default"> <action name="ltdx" class="com.qian.actions.LtDxTSAction"> <result name="success">/result.jsp</result> </action> </package></struts>
<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="ltdx.action" method="get"> username:<input style="text" name="sms.username"/><br/> password:<input style="text" name="sms.password"/><br/> <input type="submit"/> </form> </body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> username:${sms.username } password:${sms.password } </body></html>