In general, the interception of requests from the browser is implemented using filter, which enables Bean preprocessing and post-processing.
Spring MVC interceptors not only implement all the functions of filter, but also control interception accuracy more precisely.
Spring provides us with the Org.springframework.web.servlet.handler.HandlerInterceptorAdapter adapter that inherits this class and makes it very easy to implement your own interceptors. He has three methods:
[Java]View PlainCopy
- 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 {
- }
preprocessing, post processing (call service and return Modelandview, but no page rendering), return processing (page already rendered)
In the Prehandle, the coding, security control and so on can be processed;
In Posthandle, there is an opportunity to modify the Modelandview;
In aftercompletion, logging is possible based on whether an ex is null to determine if an exception has occurred.
If you use spring MVC based on an XML configuration,
You can use simpleurlhandlermapping, beannameurlhandlermapping for URL mapping (equivalent to Struts's path mapping) and interception requests (injected interceptors).
If you use spring MVC based on annotations, you can use defaultannotationhandlermapping to inject interceptors.
Note whether XML-based or annotation-based, the handlermapping bean needs to be configured in XML.
A demo:
In this example, we assume that the registration operation in Usercontroller is open only in 9:00-12:00, so you can use interceptors to implement this function.
[Java]View PlainCopy
- Public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {
- private int openingtime;
- private int closingtime;
- private String Mappingurl; //Use regular mapping to the path that needs 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", "registered opening Hours: 9:00-12:00");
- Request.getrequestdispatcher ("/msg.jsp"). Forward (request, response);
- return false;
- }
- return true;
- }
- return true;
- }
- }
XML configuration:
[HTML]View PlainCopy
- <Bean id= "timebasedaccessinterceptor" class=" Com.spring.handler.TimeBasedAccessInterceptor ">
- <property name= "openingtime" value="9" />
- <property name= "closingtime" value= " /> "
- <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 that enables the use of regular expressions to match URLs for finer-grained interception. Of course, if you do not define MAPPINGURL, all requests to the controller are blocked by default.
Usercontroller:
[Java]View PlainCopy
- @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 ...
- }
You can also configure multiple interceptors, each with a different division of work.
The use of Handlerinterceptoradapter in Spring MVC