程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Struts1 中提交中文表單到ActionForm後出現亂碼問題的原因及處理方法,struts1actionform

Struts1 中提交中文表單到ActionForm後出現亂碼問題的原因及處理方法,struts1actionform

編輯:JAVA綜合教程

Struts1 中提交中文表單到ActionForm後出現亂碼問題的原因及處理方法,struts1actionform


問題:

在你所有頁面設置的編碼都是utf-8的情況下仍然會出現中文亂碼現象。

一、亂碼出現原因

由於Struts框架直接把表單數據發送給了ActionForm,所以這裡面沒有對HttpRequestServlet進行SetCharacterEncoding,所以默認是按照ISO-8859-1。

二、解決辦法

方法 : 就是在表單提交到ActionForm之前對request進行編碼,寫一個過濾器,對所有請求進行過濾

方式一:直接在過濾器中指定編碼格式

MyFilter.java

1 public class MyFilter extends HttpServlet implements Filter { 2 private static final long serialVersionUID = 1L; 3 4 5 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException { 6 // TODO Auto-generated method stub 7 arg0.setCharacterEncoding("utf-8"); //設置接收編碼 8 arg1.setContentType("text/html;charset=utf-8"); 9 arg2.doFilter(arg0, arg1); //必須 10 11 } 12 13 14 public void init(FilterConfig arg0) throws javax.servlet.ServletException { 15 // TODO Auto-generated method stub 16 } 17 18 } View Code

在web.xml中添加

1 <filter> 2 <filter-name>MyFilter</filter-name> 3 <filter-class>com.xidian.filter.MyFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>MyFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping> View Code

 

方式二:在web.xml中指定編碼格式

MyFilter.java

1 package com.xidian.filter; 2 3 import javax.servlet.Filter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.FilterConfig; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 11 public class MyFilter extends HttpServlet implements Filter { 12 private static final long serialVersionUID = 1L; 13 private String encoding; 14 15 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException { 16 // TODO Auto-generated method stub 17 arg0.setCharacterEncoding(encoding); 18 arg2.doFilter(arg0, arg1); 19 } 20 21 public void init(FilterConfig arg0) throws javax.servlet.ServletException { 22 // TODO Auto-generated method stub 23 encoding=arg0.getInitParameter("encoding"); 24 } 25 26 } View Code

在web.xml添加

1 <filter> 2 <filter-name>MyFilter</filter-name> 3 <filter-class>com.xidian.filter.MyFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>utf-8</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>MyFilter</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> View Code

 

下面是struts2中中文亂碼的問題:

關於struts2中表單提交時,中文亂碼問題的解決

 

順帶再說下加上對輸入數據內容的過濾的情況:

再添加一個過濾程序:

SafeFilter.java

1 package com.xidian.filter; 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 11 public class SafeFilter implements Filter { 12 private String keywords[]; 13 14 public void doFilter(ServletRequest request, ServletResponse response, 15 FilterChain chain) throws IOException, ServletException { 16 //這裡我們接收到用戶名 17 String name=request.getParameter("name"); 18 request.setAttribute("err", "您不允許登錄<font color='red'>"+name+"</font>"); 19 System.out.println("第二個過濾器:"+name); 20 if(name!=null){ 21 //判斷 22 for(String key:keywords){ 23 if(key.equals(name)){ 24 request.getRequestDispatcher("WEB-INF/ifo.jsp").forward(request, response); 25 return; 26 } 27 } 28 29 } 30 chain.doFilter(request, response); 31 } 32 33 34 public void init(FilterConfig filterConfig) throws ServletException { 35 keywords=filterConfig.getInitParameter("keywords").split(";"); 36 37 } 38 39 } View Code

web.xml的配置

1 <!-- 部署過濾器的順序就是執行的順序 --> 2 <filter> 3 <filter-name>MyFilter</filter-name> 4 <filter-class>com.xidian.filter.MyFilter</filter-class> 5 <init-param> 6 <param-name>encoding</param-name> 7 <param-value>utf-8</param-value> 8 </init-param> 9 </filter> 10 11 <filter> 12 <filter-name>SafeFilter</filter-name> 13 <filter-class>com.xidian.filter.SafeFilter</filter-class> 14 <init-param> 15 <param-name>keywords</param-name> 16 <param-value>拉登;小明;鳳姐</param-value> 17 </init-param> 18 </filter> View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved