Normally , Interception of requests from browsers , It's using Filter Realized , It can be done in this way Bean Pretreatment , Post processing .
Spring MVC The interceptor of Filter All features of , It can also control the interception accuracy more accurately .

Spring For us org.springframework.web.servlet.handler.HandlerInterceptorAdapter This adapter , Inherit this class , Can be very convenient to implement their own interceptor . He has three ways :

public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler)
throws Exception {
return true;
}
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView)
throws Exception {
}
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex)
throws Exception {
}

The preprocessing is realized separately , Post processing ( Called Service And return ModelAndView, But no page rendering ), Return processing ( The page has been rendered )
stay preHandle in , It can be coded , Security control, etc ;
stay postHandle in , Opportunity to modify ModelAndView;
stay afterCompletion in , According to ex Is it null Determine if an exception has occurred , Logging .

If based on xml Configuration use Spring
MVC, It can be used SimpleUrlHandlerMapping,BeanNameUrlHandlerMapping conduct Url mapping ( amount to struts Of path mapping ) And intercept requests ( injection interceptors), If used based on annotations Spring
MVC, have access to DefaultAnnotationHandlerMapping injection interceptors. Note that no matter based on xml Or is it based on annotations ,HandlerMapping
bean All need to be in xml Configured in .

One demo:
In this case , We assume that UserController The registration operation in the 9:00-12:00 to open up , You can use interceptors to do this .

public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
private int openingTime;
private int closingTime;
private String mappingURL;// Using regular mapping to the path to be intercepted
public void setOpeningTime(int openingTime) {
this.openingTime = openingTime;
}
public void setClosingTime(int closingTime) {
this.closingTime = closingTime;
}
public void setMappingURL(String mappingURL) {
this.mappingURL = mappingURL;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String url=request.getRequestURL().toString();
if(mappingURL==null || url.matches(mappingURL)){
Calendar c=Calendar.getInstance();
c.setTime(new Date());
int now=c.get(Calendar.HOUR_OF_DAY);
if(now<openingTime || now>closingTime){
request.setAttribute("msg", " Registration opening hours :9:00-12:00");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
return false;
}
return true;
}
return true;
}
}

xml to configure :

<bean id="timeBasedAccessInterceptor"
class="com.spring.handler.TimeBasedAccessInterceptor">
<property name="openingTime" value="9" />
<property name="closingTime" value="12" />
<property name="mappingURL" value=".*/user\.do\?action=reg.*" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="timeBasedAccessInterceptor"/>
</list>
</property>
</bean>

Here we define a mappingURL attribute , Implementation using regular expression pair url Match , So as to intercept more finely . Of course, if you don't define it mappingURL, All pairs are blocked by default Controller Request for .

UserController:

@Controller
@RequestMapping("/user.do")
public class UserController{
@Autowired
private UserService userService;
@RequestMapping(params="action=reg")
public ModelAndView reg(Users user) throws Exception {
userService.addUser(user);
return new ModelAndView("profile","user",user);
}
// other option ...
}

this Controller amount to Struts Of DispatchAction

You can also configure multiple interceptors , Each interceptor has a different division of labor .

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function