Spring MVC URIEncoding can't send requestparam

2019-09-16 03:36发布

I need to send some request parameters from browser to Spring MVC controller and process them later like method parameters. The problem is that tomcat I guess didn't put right encoding for URI data which passing through. Instead of 'Имя' I'm having: %D0%9C%D0%91%D0%94%D0%9E%D0%A3+%D0%B4%2F%D1%81%E2%84%969%D1%81.+%D0%95%D0%BB%D0%B8%D0%BE%D0%BD%D0%BA%D0%B0

I use to read about this type of problem which occurs because of tomcat does not have URI encoding preinstalled.

If you mind do I have body encoding in tomcat config web.xml, so Yes I have:

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        <init-param>
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>tru?</param-value>
        </init-param>       
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

So I'm curious do I have to set up anything else to container config.? Thank you

2条回答
爷的心禁止访问
2楼-- · 2019-09-16 04:13

You have to set the URIEncoding attribute for the HTTP connector in server.xml file inside your tomcat config dir:

<Connector port="8080" URIEncoding="UTF-8" ...  />
查看更多
叼着烟拽天下
3楼-- · 2019-09-16 04:14

Only certain way I found to do this without changing Tomcat configuration is:

put parameter in form

<form onsubmit="encodeParameter(this.param)">
  <input type="text" name="param" />
  <input type="submit" />
</form>

and then encode it before submitting to server

function encodeParameter(param){
  param.value =encodeURIComponent(param.value);
}

now on server you will get correct string.

查看更多
登录 后发表回答