Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ SpringMVC the use of interceptors     - JavaScript original values and complex values (Programming)

- fcntl file locking function add (Programming)

- Using Java to build micro-services (Server)

- KVM usb passthrough configuration (Linux)

- Oracle Data Pump Example (Database)

- To install Ganglia (Linux)

- Apache Linux firewall reverse proxy configuration (Linux)

- The Rabbitmq installation under CentOS 6.4 (Linux)

- Java gets the current system time System.currentTimeMillis () (Programming)

- Linux Network Analysis Tcpdump Command Guide (Linux)

- Linux command in the dialog whiptail (Linux)

- Java Annotation Comments (Programming)

- How to make GRub instead of the default Ubuntu software center (Linux)

- C ++ inline functions (Programming)

- Ubuntu 15.04 / CentOS 7.0 to set custom boot (Linux)

- Compile Android libwebcore.so error occurs when solving (Programming)

- Preliminary understanding of SELinux security management (Linux)

- Redhat Close SELinux correct step (Linux)

- Elaborate .NET Multithreading: Using Task (Programming)

- Use custom backup plans for Debian backupninja (Linux)

 
         
  SpringMVC the use of interceptors
     
  Add Date : 2018-11-21      
         
         
         
  What is the interceptor

Interceptor generally refers generally by intercepting some requests from the browser to the server to complete some of the features section of code
Usually before a request occurs, the event, after a request we can intercept
What can be done interceptor

Interceptor can be used to verify permissions, to solve the garbage, logging operations, performance monitoring, exception handling, etc.

Custom interceptors

By inheritance Spring framework HandlerInterceptorAdapter class, then override preHandle, postHandle, afterCompletion three methods, in order to write our own business logic code implemented in three methods.

/ **
 * Custom interceptors
 * /
public class MyInterceptor extends HandlerInterceptorAdapter {
 / **
  * Before requesting execution
  * @param Request
  * @param Response
  * @param Handler indicates that the request is intercepted target
  * @return False: intercepting a request termination request true: to continue execution of the request
  * /
 @Override
 public boolean preHandle (HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {
  // Business logic code to write ... (such as: solve the garbage, the privilege verifier)
  request.setCharacterEncoding ( "utf-8");
  return true;
 }
 
 / **
  * @param ModelAndView view can be operated
  * /
 @Override
 public void postHandle (HttpServletRequest request, HttpServletResponse response,
       Object handler, ModelAndView modelAndView) throws Exception {
  // Business logic code to write ... (eg: Operation logging, change the view information)
 }
 
 / **
  * @param Ex abnormalities
  * /
 @Override
 public void afterCompletion (HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {
  // Business logic code to write ... (such as: resource destruction, exception handling)
 }
}

a) public boolean preHandle (HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception
The method of doing before action can be achieved pretreatment data, such as: coding, anti resubmit security control.
If the method returns true, then proceed to action. Return false then intercepts the request

b) public void postHandle (HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception
The method in the action execution, before generating execution view. Here we have the opportunity to modify the view layer data.

c) public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
Final performance, typically for releasing resources to handle exceptions. We can is empty according to ex, related to exception handling.

Register interceptor

In the xml configuration file:

 < Mvc: interceptors>
  < Bean id = "myInterceptor" class = "com.admol.web.MyInterceptor" />
< / Mvc: interceptors>

When there are multiple interceptors when you can write multiple bean
When we want to intercept some of the designated request:

 < Mvc: interceptors>
      < -! Path: the need to intercept the request, it will intercept all requests not write ->
  < Mvc: mapping path = ". / * Htm" />
  < Bean id = "myInterceptor" class = "com.admol.web.MyInterceptor" />
< / Mvc: interceptors>

A plurality of interceptors

xml configuration:

 < Mvc: interceptors>
  < Bean id = "myInterceptor" class = "com.admol.web.MyInterceptor" />
  < Bean id = "myInterceptor2" class = "com.admol.web.MyInterceptor2" />
< / Mvc: interceptors>

When configured with a plurality of interceptors, will perform preHandle method according to the order of interceptors, then reverse and perform postHandle afterCompletion methods.

Other implementations
1. Implement the interface implements HandlerInterceptor
2. implement the interface implements WebRequestInterceptor
Sign interceptor method unchanged

The difference between the filter

1. The filter is dependent on the Servlet container, based on the callback function, Intercepto dependent on the frame, based on reflection
2. The larger filter filter range, you can also filter some static resources interceptor intercepts requests
     
         
         
         
  More:      
 
- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)
- Cancel Root Command History under Linux (Linux)
- Use Aptik Battery Monitor monitoring Ubuntu laptop battery usage (Linux)
- Linux operating system buffer overflow attacks Countermeasures (Linux)
- Free compiler install MySQL-5.6.14 (Database)
- Use Docker containers (Linux)
- Git 2.5 increase in the working tree, triangle improved workflow performance (Linux)
- The difference between equals and == in Java (Programming)
- A new method for Linux hidden files (Linux)
- C # asynchronous delegates (Programming)
- Compile and install GCC 4.8.1 + GDB 7.6.1 + Eclipse in CentOS 6.4 in (Linux)
- Oracle database online redo logs are several methods of recovery of deleted (Database)
- Oracle 11g users to create, authorize and import dmp file (Database)
- Linux mount command Detailed (Linux)
- CentOS7 iptables (Linux)
- To create a problem by the statement date comparison of the difference between MySQL and Oracle (Database)
- Linux command Detailed chpasswd bulk edit user password (Linux)
- Install minimize RHEL / CentOS 7 things to do (Linux)
- Approach the next Linux shared interrupts (Linux)
- To achieve Linux Security (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.