我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)

          我的前臺(tái)頁(yè)是這樣的:
          <body>
                
          <form action="test.action" method="post">
                    測(cè)試文件:
          <input type="file" id="doc" name="path" value=""/>
                    
          <input type="submit" value="提交" onclick=""/>
                
          </form>
            
          </body>
          Action:
          package com;

          import java.io.UnsupportedEncodingException;


          import com.opensymphony.xwork2.ActionSupport;

          @SuppressWarnings(
          "serial")
          public class TestAction extends ActionSupport
          {
              
          private String path;

              
          public String getPath()
              
          {
                  
          return path;
              }


              
          public void setPath(String path)
              
          {
                  
          this.path = path;
              }



              
          public String test() throws Exception
              
          {
                  System.out.println(path.replace(
          "\\""\\\\"));
                  
          return SUCCESS;
              }


          }


          剛開始的時(shí)候一選中文路徑就輸出???.
          后來終于找到解決方法.
          在struts.xml文件中加上:
          為了解決form提交到action中的中文參數(shù)亂碼問題。
           
          1.在struts2-core-2.0.0-SNAPSHOT.jar包中路徑為struts2-core-2.0.6\org\apache \struts2
          有一個(gè)default.properties 文件,把struts.i18n.encoding=UTF-8改為
          struts.i18n.encoding=GBK 

          2.或者在struts.xml文件內(nèi)添加常量:
          <constant name="struts.i18n.encoding" value="GBK"/>

          我當(dāng)然是用的第二種方法,簡(jiǎn)單方便.
          <?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="GBK"/>
              
          <package name="com" extends="struts-default">
                  
          <action name="test" class="com.TestAction" method="test">
                      
          <result>/ok.jsp</result>
                  
          </action>
              
          </package>
          </struts>

          呵呵,終于解決了.希望對(duì)大家有些幫助.

          posted on 2007-12-26 12:24 々上善若水々 閱讀(12705) 評(píng)論(5)  編輯  收藏 所屬分類: Struts2

          評(píng)論

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評(píng)論   

          呵呵 謝謝 !
          2008-12-22 15:46 | 雪楓

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評(píng)論   

          不錯(cuò) 問題解決。
          2009-05-15 21:47 | 杜棟

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑) [未登錄]  回復(fù)  更多評(píng)論   

          真的解決了,謝謝你啊,牛逼
          2011-05-26 06:06 | rose

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評(píng)論   

          太謝謝你了,解決了困擾幾天的問題。
          2012-09-27 11:10 | Ajian

          # re: struts2中form提交到action中的中文參數(shù)亂碼問題解決辦法(包括取中文路徑)   回復(fù)  更多評(píng)論   

          非常感謝,解決了我花費(fèi)一天時(shí)間都沒解決的問題
          2013-05-06 18:46 | *
          主站蜘蛛池模板: 北川| 健康| 屏东市| 九龙县| 九寨沟县| 五常市| 时尚| 唐河县| 安塞县| 高淳县| 临沂市| 尚义县| 无棣县| 唐河县| 石城县| 南充市| 丹巴县| 利川市| 江西省| 娱乐| 玉树县| 天全县| 湖州市| 门头沟区| 中宁县| 汉寿县| 诸城市| 博客| 五大连池市| 黄冈市| 宣武区| 山东省| 浪卡子县| 罗田县| 车致| 桦甸市| 安阳县| 塘沽区| 保定市| 宜兴市| 远安县|