Java笔记之SpringMVC(四):中文乱码问题

0.说在前面

  基于SpringMVC--注解项目

1.新建encoding.jsp和success.jsp页面

encoding.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>中文乱码问题</title>
</head>
<body>
    <form action="encoding.action" method="post">
        姓名:<input type="text" name="name"/>
        <button type="submit">提交</button>
    </form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
${message }
</body>
</html>

2.新建EncodingController类

package com.springmvc.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EncodingController {

    @RequestMapping("/initEncoding.action")
    public ModelAndView init(){
        ModelAndView mav=new ModelAndView("encoding");
        return mav;
    }
    
    @RequestMapping("/encoding.action")
    public ModelAndView doSubmit(String name){
        System.out.println("提交的信息为:"+name);
        ModelAndView mav=new ModelAndView("success");
        mav.addObject("message", "提交的信息为:"+name);
        return mav;
    }
}

3.访问http://localhost:8080/springmvc_demo/initEncoding.action,跳转到encoding.jsp页面

4.在输入框中分别输入abc和张三,查看页面展示信息

输入abc

输入张三

可见,输入中文出现了乱码问题.

5.解决办法

修改web.xml文件,添加post提交编码过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
  <!-- 配置SpringMVC的前端控制器 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <!-- 声明SpringMVC配置文件的位置 -->
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <!-- 所有以action结尾的请求都交给SpringMVC处理 -->
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <!-- 设置post提交编码过滤器 -->
  <filter>
      <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <!-- 设置编码方式为UTF-8 -->
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <!-- 对所有请求都进行拦截 -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <display-name>springmvc_demo</display-name>
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

再输入张三,进行提交

从页面展示信息可以看出中文乱码问题得到解决.

posted @ 2020-04-07 15:49  安徒生敲代码  阅读(327)  评论(0编辑  收藏  举报