我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

我的前台页是这样的:
<body>
      
<form action="test.action" method="post">
          测试文件:
<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;
    }


}


刚开始的时候一选中文路径就输出???.
后来终于找到解决方法.
在struts.xml文件中加上:
为了解决form提交到action中的中文参数乱码问题。
 
1.在struts2-core-2.0.0-SNAPSHOT.jar包中路径为struts2-core-2.0.6\org\apache \struts2
有一个default.properties 文件,把struts.i18n.encoding=UTF-8改为
struts.i18n.encoding=GBK 

2.或者在struts.xml文件内添加常量:
<constant name="struts.i18n.encoding" value="GBK"/>

我当然是用的第二种方法,简单方便.
<?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>

呵呵,终于解决了.希望对大家有些帮助.

posted on 2007-12-26 12:24 々上善若水々 阅读(12679) 评论(5)  编辑  收藏 所属分类: Struts2

评论

# re: struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)   回复  更多评论   

呵呵 谢谢 !
2008-12-22 15:46 | 雪枫

# re: struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)   回复  更多评论   

不错 问题解决。
2009-05-15 21:47 | 杜栋

# re: struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径) [未登录]  回复  更多评论   

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

# re: struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)   回复  更多评论   

太谢谢你了,解决了困扰几天的问题。
2012-09-27 11:10 | Ajian

# re: struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)   回复  更多评论   

非常感谢,解决了我花费一天时间都没解决的问题
2013-05-06 18:46 | *

只有注册用户登录后才能发表评论。


网站导航: