Home  >  Q&A  >  body text

java - springmvc接收url中文参数乱码

1.今天在做一个例子的时候,发现后台不能正确接收中文的url参数,试了各种解决办法都不可以。

以下是代码:
Controller:

package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world IDEA!");
        model.put("content","This is my first springmvc web");
        return "index";
    }
    @RequestMapping(value = "/page/{name}/{age}",method = RequestMethod.GET)
    public String getName(ModelMap modelMap, @PathVariable("name") String name, @PathVariable("age") int age) {
        modelMap.addAttribute("name",name);
        modelMap.addAttribute("age",age);
        return "name";
    }
}

name.jsp

<%@ page pageEncoding="UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <p>
        名字:${name}<br/>
        年龄:${age}<br/>
    </p>
</body>
</html>

web.xml

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Encoding -->
    <filter>
        <filter-name>characterEncodingFilter</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>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
PHP中文网PHP中文网2742 days ago424

reply all(9)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 18:01:59

    Most of the solutions have been explained clearly above. Let me sort it out in 3 ways

    1. Tomcat can be configured directlyURIEconding="UTF-8"

    2. new String("中文".getBytes("ISO-8859-1"), "UTF-8");

    3. Use URLEncoderencoding in Chinese like: baidu.com/s?wd=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C

    These 3 methods can solve the problem of garbled Chinese URLs.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 18:01:59

    Get submission uses the constructor method of string class

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 18:01:59

    Have you debugged it to see if the data received in the background is garbled? Or is the database garbled? The two are different situations. For the former, you can try to transcode the parameters. If you see that the background code is not garbled and the database is garbled, then check whether you forgot to add enConding on the database link...request.setCharacterEncoding方法,并且用new String(para.getBytes(“ISO-8859-1”),“UTF-8”

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 18:01:59

    My approach is

    Encapsulate it into an object, then use @RequestBody to annotate the parameter, and then get it from the object. However, sometimes when there are few parameters, it feels unnecessary to encapsulate an object again

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 18:01:59

    springmvc Chinese garbled problem: http://luanxiyuan.iteye.com/blog/1849169
    new String(received parameters.getBytes("ISO-8859-1"), "UTF-8");

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 18:01:59

    Has the character set of the container been specified?

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 18:01:59

    If it is a tomcat container, please configure it URIEconding="UTF-8" useBodyEncodingForURI="true"

        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000" URIEconding="UTF-8" useBodyEncodingForURI="true"
                   redirectPort="8443" />

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 18:01:59

    You can use Base64 encoding on the front end and then send it to the backend for decoding

    reply
    0
  • 迷茫

    迷茫2017-04-17 18:01:59

    @RequestMapping(value = "/xxx", method = RequestMethod.GET, headers = {"content-type=application/json;charset=UTF-8"}, produces = {"application/json;charset=UTF-8"})

    reply
    0
  • Cancelreply