`

过滤器解决Struts2中的中文乱码问题

阅读更多

初学Struts2,就编写了一个简单的登录的例子,在运行中遇到当用户名为中文时出现中文乱码,故上网搜索解决之道,并撰此文以记之。


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>
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics