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

一、乱码问题

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,然后提交表单。可以发现乱码问题已解决。

输出:

你好呀

相关

我的该分类的其他相关文章,请点击:【Spring + Spring MVC + MyBatis】文章目录

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值