长生的梦呓


  • 归档
  • 算法
  • 基础
  • 关于

  • 分类
  • 日志
  • Servlet
  • Archive
  • 数据结构
  • IO 流

  • 标签
  • 友链
  • MyBatis
  • About
  • Spring 5
  • Java SE
  • Java EE
  • Algorithms
  • 新特性
  • 位运算技巧

  • 搜索
内网穿透 项目实战 数据库 MySQL 安卓踩坑 开发工具 设计模式 Enum 枚举 Linux MyBatis-plus JSON IDEA Transactions AOP IO 流 DP IoC 与 DI 位运算技巧 工具类 学习技巧 Git JDK 排序 Spring Boot Spring MVC Spring Framework MyBatis Log4J Regex Jsoup JDBC 数据结构 递推 递归 算法 Servlet与JSP 小难 中等 简单

【Spring】(15)Spring MVC 乱码问题

发表于 2020-04-09 | 3 | 阅读次数 294

一、乱码问题

1.项目结构

在这里插入图片描述

2.配置文件

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个SpringMVC的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--服务器启动的时候就启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <!--  "/"匹配所有的请求不包括.jsp。"/*"匹配所有的请求包括.jsp -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

springmvc-servlet.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描包,让指定包下的注解生效,有IoC容器统一管理 -->
    <context:component-scan base-package="com.shengjava.web"/>
    <!--开启注解扫描-->
    <mvc:annotation-driven/>
    <!--在web开发中,我们一般存在静态资源css,js,img。。。-->
    <mvc:default-servlet-handler/>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

3.控制器

TestController控制器

@Controller
public class TestController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @PostMapping("/show")
    public String test(@RequestParam String name, Model model) {
        // 将数据传到前端
        model.addAttribute("name", name);
        return "show";
    }
}

4.jsp页面

index.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<form action="show" method="post">
    <input type="text" name="name">
    <input type="submit">
</form>
</body>
</html>

show.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${name}
</body>
</html>

5.测试

如果此时我们部署到tomcat,访问/index路径,输入中文"你好呀"后提交,show.jsp页面返回的会是中文乱码。

输出如:

ä½ å¥½å‘€

怎么解决这个问题呢?

二、解决

SpringMVC为我们提供了一个过滤器,可以在web.xml中配置。

在web.xml中,添加如下过滤器代码。就可以解决。

    <!-- 配置Spring MVC编码过滤器(解决乱码) -->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <!--  "/"匹配所有的请求不包括.jsp。"/*"匹配所有的请求包括.jsp -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

测试:访问/index,然后提交表单。可以发现乱码问题已解决。

输出:

你好呀
  • 本文作者: 长生的梦呓
  • 本文链接: https://shengjava.com/archives/spring15springmvc乱码问题
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
# 内网穿透 # 项目实战 # 数据库 # MySQL # 安卓踩坑 # 开发工具 # 设计模式 # Enum # 枚举 # Linux # MyBatis-plus # JSON # IDEA # Transactions # AOP # IO 流 # DP # IoC 与 DI # 位运算技巧 # 工具类 # 学习技巧 # Git # JDK # 排序 # Spring Boot # Spring MVC # Spring Framework # MyBatis # Log4J # Regex # Jsoup # JDBC # 数据结构 # 递推 # 递归 # 算法 # Servlet与JSP # 小难 # 中等 # 简单
【Spring】(14)Spring MVC 使用注解 + RESTful风格
【Spring】(16)Spring MVC 拦截器
  • 文章目录
  • 站点概览
长生的梦呓

长生的梦呓

110 日志
39 分类
40 标签
RSS
E-mail CSDN
Creative Commons
Links
  • CSDN 地址
  • waltz26
  • Ryan Wang's Blog
  • JohnNiang's Blog
  • 廖雪峰
  • 菜鸟教程
© 2021 长生的梦呓
浙ICP备20005262号-1