Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ SpringMVC the use of interceptors     - C ++ Supplements - Virtual Function Principle (Programming)

- ORA-28000 the account is locked fault simulation (Database)

- Sublime Text 3 best features, plug-ins and settings (Linux)

- Linux System Getting Started Tutorial: How do you know Shell which is currently being used (Linux)

- Shell Programming Regular Expressions (Programming)

- Ubuntu 14.10 / Linux Mint 17.1 Win10 compiler install rdesktop 1.8.3 Remote Desktop connection (Linux)

- Linux Demo dd IO test (Linux)

- Btrfs file system repair techniques (Linux)

- Customize own small private Linux system (Linux)

- Ubuntu program using the Sound Recorder (Linux)

- How to install the Linux text editor Atom 0.124.0 (Linux)

- Magical Virtualbox under Ubuntu (Linux)

- Programmer editor Vim (Linux)

- About Nginx 7 (Server)

- ogg Oracle to SQL Server 2005 to achieve synchronization (Database)

- nginx.conf Optimization (Server)

- Java string concatenation techniques (StringBuilder tips) (Programming)

- Nginx server security configuration (Server)

- HomeKit Human Interface Guidelines (Linux)

- Git Rebase Tutorial: Using Git Rebase turn back the clock (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:      
 
- Linux raw socket (Programming)
- ctop: monitor container performance Linux command line artifact (Linux)
- Linux disk partition, format, mount the directory (Linux)
- Iptables use examples (Linux)
- Use mysqldump backup performed MariaDB (Database)
- Linux account related documents Interpretation (Linux)
- iptraf: A Practical TCP / UDP network monitoring tools (Linux)
- The Linux role of each directory contents (Linux)
- Using Linux command line and execute PHP code (Programming)
- Linux SVN account password to save your settings (Linux)
- Circular list of Java programming (Programming)
- To create someone else can not afford to delete the administrator user (Linux)
- Linux kernel boot to retain large memory method summary (Linux)
- MySQL Error Code Complete (Database)
- Implement firewall function on a closed Linux machine (Linux)
- MySQL thread cache thread_cache_size parameter optimization (Database)
- 10 easy to use Linux utilities Recommended (Linux)
- Overall Physical Migration of Oracle Database with (Database)
- CentOS 7 How to install MySQL Server (Database)
- CentOS 6 rapid deployment of Java applications (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.