struts 2でformフォームがaction乱符号にコミットされます

34529 ワード

java 1234官网!!!
今日は文字化けしの問題に遭遇した.jspページコードは以下の通りであり、ページコードはUTF-8である.
 1 @ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 @taglib uri="/struts-tags" prefix="s" %>
 3 
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="">
12     
13     <title>My JSP 'Category_input.jsp' starting pagetitle>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     
23 
24   head>
25   
26   <body>
27   <form action="admin/Category-update" method="post">
28   <input type="hidden" name="category.id" value='value="category.id"/>'>
29       name:<input name="category.name" value='value="category.name"/>'/>
30       description:<textarea name="category.description"><s:property value="category.description"/>textarea>
31       <input type="submit" value="update" /> 
32   form>
33   body>
34 html>

Web.xmlの構成は次のとおりです.
 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>bbs2display-name>
 4  <welcome-file-list>
 5     <welcome-file>index.jspwelcome-file>
 6   welcome-file-list>
 7   
 8   <filter>
 9         <filter-name>struts2filter-name>
10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
11     filter>
12 
13     <filter-mapping>
14         <filter-name>struts2filter-name>
15         <url-pattern>/*url-pattern>
16     filter-mapping>
17   
18 web-app>

struts.xmlの構成は次のとおりです.
 1 xml version="1.0" encoding="UTF-8" ?>
 2 DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7 
 8     
 9     <constant name="struts.i18n.encoding" value="UTF-8" />
10     
11     <constant name="struts.devMode" value="true" />
12     
13     
14     <package name="front" namespace="/" extends="struts-default">
15         
16         <action name="Category-list" class="com.lch.bbs2009.action.CategoryAction">
17             <result>/admin/Category-list.jspresult>
18         action>
19     package>
20     
21     <package name="admin" namespace="/admin" extends="struts-default">
22         
23         <action name="index">
24             <result>/admin/index.htmlresult>
25         action>
26         
27         <action name="*-*" class="com.lch.bbs2009.action.{1}Action" method="{2}">
28             <result name="success">/admin/{1}-{2}.jspresult>
29             <result name="input">/admin/{1}-{2}.jspresult>
30         action>
31 
32     package>
33 
34 struts>

アクションコードは次のとおりです.
 1 package com.lch.bbs2009.action;
 2 
 3 import java.util.List;
 4 
 5 import com.lch.bbs2009.model.Category;
 6 import com.lch.bbs2009.service.CategoryService;
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 /**
10  * @author licheng
11  * 
12  */
13 public class CategoryAction extends ActionSupport {
14     /**
15      * 
16      */
17     private List categories;
18     private CategoryService categoryService = new CategoryService();
19     private Category category;
20     private int id;
21 
22     public String list() {
23         categories = categoryService.queryAll();
24         return SUCCESS;
25     }
26 
27     public String add() {
28         categoryService.add(category);
29         return SUCCESS;
30     }
31 
32     public String update() {
33         categoryService.update(category);
34         return SUCCESS;
35     }
36 
37     public String delete() {
38         categoryService.deleteById(id);
39         return SUCCESS;
40     }
41 
42     public String addInput() {
43         return INPUT;
44     }
45 
46     public String updateInput() {
47         this.category = this.categoryService.loadById(id);
48         return INPUT;
49     }
50     
51     public List getCategories() {
52         return categories;
53     }
54 
55     public void setCategories(List categories) {
56         this.categories = categories;
57     }
58 
59     public CategoryService getCategoryService() {
60         return categoryService;
61     }
62 
63     public void setCategoryService(CategoryService categoryService) {
64         this.categoryService = categoryService;
65     }
66 
67     public Category getCategory() {
68         return category;
69     }
70 
71     public void setCategory(Category category) {
72         this.category = category;
73     }
74 
75     public int getId() {
76         return id;
77     }
78 
79     public void setId(int id) {
80         this.id = id;
81     }
82 
83 }

デバッグモードでデバッグすると、actionで受信した値が文字化けしていることがわかります.
ネットで解決策を探す
1.ページエンコーディング、サーバエンコーディング、データベースエンコーディングが一致しているかどうかを確認します.私のはすべてUTF-8です.
2.プロファイルの前に書いてください.書きましたが、無効です.
3.struts.propertiesに追加
struts.locale=zh_CNstruts.i 18 n.encoding=UTF-8は、locale、encoding文字セットを設定します.
补充:struts.propertiesは要らないことができます!!struts 2のデフォルトプロファイルはdefault.propertiesで、struts 2-core-2.x.jarのorg.apache.struts 2パッケージの下にあります.1つのプロジェクトのプロパティが両方のファイルで構成されている場合、struts.xmlを先にロードし、struts.propertiesをロードするロード順序があります.すなわちstruts.propertiesはstruts.xmlの構成を上書きすることができる.具体的にstruts.propertiesを使うかどうかは、状況によって異なります.
このファイルを追加して変更しましたが、無効な結果になりました.
4.web.xmlに追加
 
 1 
 2     <filter>
 3         <filter-name>struts-cleanupfilter-name>
 4         <filter-class>
 5             org.apache.struts2.dispatcher.ActionContextCleanUp
 6         filter-class>
 7     filter>
 8     <filter-mapping>
 9         <filter-name>struts-cleanupfilter-name>
10         <url-pattern>/*url-pattern>
11     filter-mapping>

 
 
 
補足:web.xmlのEncodingfilterの位置は、Struts 2のFilterDispatcherの前にする必要があります.文字セットを調整してActionに入るためです.Struts 2のAPIに従って、filterの順序は次のとおりです.
struts-cleanup filterSiteMesh filterFilterDispatcher
無効な結果
5.フォームコミットはservlet調post()またはget()メソッドで処理され、結果をクライアントに返します.フィルタクラスを書くと、フォームのコミットはservletリクエストに先行し、リクエストの前にフィルタを追加すると、文字を中国語に変換して処理します.
 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
 
HttpServletRequest request_1 = (HttpServletRequest) request; HttpServletResponse response_1 = (HttpServletResponse) response;
 
//符号化を変換し、キャッシュrequest_1.setCharacterEncoding(「GBK」);response_1.setCharacterEncoding(「GBK」);chain.doFilter(request,response);
 
}私はこのようにservletを建てた後、上は赤いフォークです!!
谁が私にどのように解决することを教えることができて、ありがとうございます!!
 
私のお父さんは午后をして、最后に问题はバージョンの问题を解决しました!!!
 問題が解決しましたstrutsバージョンの問題
Struts 2.1.8バージョンの前にBugがあり、中国語が文字化けしています.GBKにしてもいいようなので、葛藤したくないので、jarバッグを直接交換しましょう!!馬兵士の先生が前に話したのを覚えていますが、残念ながら忘れました.午後中やったが、ずいぶん勉強になった.呼、ここで小锋先生が远隔で问题を解决してくれたことに感谢します!!!
 
 
 
 
 
 
 
 
 
 
転載先:https://www.cnblogs.com/ligui989/p/3219773.html