SpringMVCは、文字セットのフィルタを登録し、文字化けの問題を解決するための要求を投稿します

<?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">
  <display-name>01-primary</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 注册springmvc的核心对象DispatherServlet(中央调度器)  -->
  
  <!-- 
      /WEB-INF/springmvc-servlet.xml
      /WEB-INF/myweb-servlet.xml
      
      DispatcherServlet在创建对象的时候,在init()方法中会创建springmvc的容器对象
      WebApplicationContext, 创建容器的时候会读取配置文件, 创建配置文件中的所有bean对象。
           默认读取的配置文件的位置是 /WEB-INF ,默认的文件名称是 <servlet-name>-servlet.xml
  
   -->
  <servlet>
  	<servlet-name>myweb</servlet-name>
  	<!-- springmvc.jar -->
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 自定义springmvc配置文件的位置和名称 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	
  	<!-- 在服务器启动的时候,创建对象 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>myweb</servlet-name>
  	<!-- 
  	    url-pattern是把请求交给某个Servlet处理, 我们现在要做的是把用户的请求交给SpringMVC框架中的核心对象
  	    中央调度器, 交给中央调度器的处理的请求才能使用springmvc框架。
  	       中央调度器是springmvc框架的入口
  	   
  	    url-pattern的配置方式:
  	    1.扩展名方式: *.xxxx ; xxxx是自定义的扩展名,例如 *.do , *.action, *.mvc的等等。不能使用 *.jsp
  	      http://localhost:8080/myweb/some.do
  	      http://localhost:8080/myweb/user/addUser.do
  	    2.使用斜杠"/"
  	 -->
     <url-pattern>*.do</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>
  		<param-value>utf-8</param-value>
  	</init-param>
  	<!-- 强制请求(HttpServletRequest)对象,使用encoding的字符编码 -->
  	<init-param>
  		<param-name>forceRequestEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	
  	<!-- 强制应答(HttpServletResponse)对象,使用encoding的字符编码 -->
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  
</web-app>
公開された388元の記事 ウォン称賛40 ビュー10万+

おすすめ

転載: blog.csdn.net/qq_30347133/article/details/104561577