一、乱码问题
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】文章目录