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

 小子转过来_406 发布于 2022-10-30 16:41

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" %>


    


    

名字:${name}
年龄:${age}

web.xml

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    Spring MVC Application

    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        mvc-dispatcher
        /
    

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    

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

    2022-10-31 21:38 回答
  • 可以在前端用Base64编码后传到后台解码

    2022-10-31 21:38 回答
  • 如果是 tomcat 容器的话,请配置 URIEconding="UTF-8" useBodyEncodingForURI="true"

        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000" URIEconding="UTF-8" useBodyEncodingForURI="true"
                   redirectPort="8443" />
    2022-10-31 21:38 回答
  • 容器的字符集指定了吗

    2022-10-31 21:38 回答
  • springmvc中文乱码问题:http://luanxiyuan.iteye.com/blog/1849169
    new String(接收到的参数.getBytes("ISO-8859-1"), "UTF-8");

    2022-10-31 21:38 回答
  • 我的做法是

    封装到一个对象里,然后用@RequestBody 来注解这个参数,然后从对象里get出来,但是有时候参数很少的时候,再去封装一个对象,感觉多此一举

    2022-10-31 21:38 回答
  • 有没有debug下看后台接收到的数据是乱码?还是到数据库乱码了?两者是不同的情况,前者可以尝试request.setCharacterEncoding方法,并且用new String(para.getBytes(“ISO-8859-1”),“UTF-8”对参数进行转码试试,如果看到后台的不是乱码,到数据库乱码那就看数据库链接上是不是忘了加enConding…

    2022-10-31 21:38 回答
  • get提交用 string类的构造方法

    2022-10-31 21:38 回答
  • 大部分解决方式楼上都已经讲得比较清楚了。我把3种方式整理一下吧

    1. Tomcat 可以直接配置URIEconding="UTF-8"

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

    3. 将中文使用URLEncoder编码如:baidu.com/s?wd=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C

    这3种方式都可以解决中文URL乱码问题。

    2022-10-31 21:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有