【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】文章目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章