Unit05: 实战技巧 、 资费列表 、 拦截器

  Unit05: 过滤器解决表单写中文乱码拦截器    

1. 使用过滤器解决表单中文参数值乱码问题

注意:
a. 表单提交方式必须为POST。
b. 过滤器的编码应该与浏览器端设置的编码一致。

2. 拦截器

(1)什么是拦截器?

spring框架当中的一种特殊的组件,当前端控制器(DispatcherServlet)调用处理器(Controller)之前,会先调用拦截器,然后再调用处理器。
注:
过滤器属于Servlet规范当中定义的组件,而拦截器属于Spring框架 当中定义的组件。

(2)如何写一个拦截器?

step1. 写一个java类,实现HandlerInterceptor接口。
step2. 在接口方法当中,实现拦截处理逻辑。

step3. 配置拦截器。

拦截器代码:

src/main/java

controller

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    @RequestMapping("/toHello.do")
    public String toHello(){
        System.out.println("toHello()");
        return "hello";
    }
    
    @RequestMapping("/demo/toHello2.do")
    public String toHello2(){
        System.out.println("toHello2()");
        return "hello";
    }
}
HelloController.java

interceptors

package interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SomeInterceptor implements 
HandlerInterceptor{

    /**
     * 最后执行的方法。
     * ex:处理器所抛出的异常。
     */
    public void afterCompletion(
            HttpServletRequest request, 
            HttpServletResponse response, 
            Object arg2, Exception ex)
            throws Exception {
        System.out.println("afterCompletion()");
    }

    /**
     * 在处理器已经执行完毕,正准备将处理结果
     * (ModelAndView)返回给前端控制器之前,执行
     * postHandle方法。
     * 注:可以在该方法里面,修改处理结果。
     */
    public void postHandle(
            HttpServletRequest request, 
            HttpServletResponse response, 
            Object arg2, ModelAndView mav)
            throws Exception {
        System.out.println("postHandle()");
    }

    /**
     * 如果该方法返回值为true,表示继续向后
     * 调用(即调用后面的拦截器和处理器);
     * 如果该方法的返回值为false,则不再向后
     * 调用,返回处理结果。
     * arg2: 处理器方法对象。
     */
    public boolean preHandle(
            HttpServletRequest request, 
            HttpServletResponse response,
            Object arg2) throws Exception {
        System.out.println("preHandle()");
        return true;
    }
    
}
SomeInterceptor.java

src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan 
    base-package="controller"/>
    <!-- 配置视图解析器 -->
    <bean
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/> 
    <property name="suffix" value=".jsp"/> 
    </bean> 
    
    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean 
            class="interceptors.SomeInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    
</beans>
spring-mvc.xml

WEB-INF

<h1>Hello,SpringMVC</h1>
Hello.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 
         DispatcherServlet的初始化方法在执行时,
         会启动spring容器。
         contextConfigLocation负责指定spring
         配置文件的位置。
     -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
  
</web-app>
web.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tarena.spring</groupId>
  <artifactId>springmvc03</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>3.2.8.RELEASE</version>
      </dependency>
  </dependencies>
</project>
pom.xml

在表单提交时,如果遇到中文字符会出现乱码现象,String提供了一个CharacterEncodingFilter过滤器,可用于解决乱码问题。

CharacterEncodingFilter使用时需要注意以下问题:

  •    表单数据以POST方式提交;  
  •  在web.xml中配置CharacterEncodingFilter过滤器;
  • 页面编码和过滤器指定编码要保持一致;
web.xml

<
filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
原文地址:https://www.cnblogs.com/tangshengwei/p/6512498.html