• 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.6orgapache struts2
    有一个default.properties 文件,把struts.i18n.encoding=UTF-8改为

    struts.i18n.encoding=GBK 


    2.或者在struts.xml文件内添加常量: 

    <constant name="struts.i18n.encoding" value="GBK"/>

    我设置value="GB2312"


    我当然是用的第二种方法,简单方便.

    <?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>


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

    http://www.blogjava.net/supercrsky/articles/170549.html

  • 相关阅读:
    【一句日历】2020年4月
    【2020-03-31】思维永远只有一种
    【2020-03-30】事情就怕太容易而看不起
    【2020-03-29】人生有趣的地方在于不断升级自己
    【2020-03-27】球场还在,换了地方而已
    【2020-03-26】人其实是一个系统
    【2020-03-25】没工作是个伪命题
    day 74 vue 2 axios数据请求 以及组件的学习
    day 73 初学vue (1)
    day 72 crm(9) 客户关系系统,整体实现,以及自定制路由内容,客户关系梳理
  • 原文地址:https://www.cnblogs.com/yuhuameng/p/5448402.html
Copyright © 2020-2023  润新知