springmvc中以get方式请求出现乱码问题,总是有???问号出现,求大神帮忙。

web.xml中的内容

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
    <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet> 
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Controller中的内容

 package cn.sxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sxt.vo.User;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(String name,ModelMap model){
        model.addAttribute("name",name);
        System.out.println(name);
        return "index.jsp";
    }

}

index.jsp中的内容

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    ${name } <br/>欢迎访问!
  </body>
</html>

tomcat7的server.xml中也加上了URIEncoding="UTF-8",还是乱码,以post表单形式提交就没问题,我的请求url地址:http://localhost:8080/07springmvc_restful/hello.do?name=张三

求大神,帮忙,多谢,多谢,多谢,多谢,多谢,多谢。

3个回答

我感觉没啥问题,,试着改下浏览器编码,,,,

greenapple222
greenapple222 嗯嗯,多谢。
大约 4 年之前 回复
Royal_lr
Royal_lr 回复greenapple222: 自带的浏览器可能不那么完善,没必要纠结
大约 4 年之前 回复
greenapple222
greenapple222 你说的对,改了下浏览器的编码为utf-8,就可以了,但是MyEclipse自带的web browser编码也是utf-8的居然还是乱码,也是醉了,多谢。
大约 4 年之前 回复

MyEclipse自带的浏览器还是有很多问题的

filter只能解决post请求的乱码,get请求的需要使用name = new String(name.getBytes("iso-8859-1"),"utf-8");

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐