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>

    <div>

        名字:${name}<br/>

        年龄:${age}<br/>

    </div>

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



Smart猫小萌
浏览 973回答 7
7回答

手掌心

get提交用 string类的构造方法

有只小跳蛙

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

小怪兽爱吃肉

如果是 tomcat 容器的话,请配置 URIEconding="UTF-8" useBodyEncodingForURI="true"&nbsp; &nbsp; <Connector port="8080" protocol="HTTP/1.1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;connectionTimeout="20000" URIEconding="UTF-8" useBodyEncodingForURI="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;redirectPort="8443" />

呼啦一阵风

@RequestMapping(value = "/xxx", method = RequestMethod.GET, headers = {"content-type=application/json;charset=UTF-8"}, produces = {"application/json;charset=UTF-8"})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java