The use of Handlerinterceptoradapter in Spring MVC

Source: Internet
Author: User

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
  1. Public Boolean Prehandle (httpservletrequest request, httpservletresponse response, Object handler)
  2. throws Exception {
  3. return true;
  4. }
  5. public void Posthandle (
  6. HttpServletRequest request, HttpServletResponse response, Object handler, Modelandview Modelandview)
  7. throws Exception {
  8. }
  9. public void Aftercompletion (
  10. HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
  11. throws Exception {
  12. }


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
  1. Public class Timebasedaccessinterceptor extends Handlerinterceptoradapter {
  2. private int openingtime;
  3. private int closingtime;
  4. private String Mappingurl; //Use regular mapping to the path that needs to be intercepted
  5. public void setopeningtime (int openingtime) {
  6. this.openingtime = openingtime;
  7. }
  8. public void setclosingtime (int closingtime) {
  9. this.closingtime = closingtime;
  10. }
  11. public void Setmappingurl (String mappingurl) {
  12. This.mappingurl = Mappingurl;
  13. }
  14. @Override
  15. Public Boolean prehandle (httpservletrequest request,
  16. HttpServletResponse response, Object handler) throws Exception {
  17. String Url=request.getrequesturl (). toString ();
  18. if (mappingurl==null | | url.matches (MAPPINGURL)) {
  19. Calendar c=calendar.getinstance ();
  20. C.settime (new Date ());
  21. int Now=c.get (calendar.hour_of_day);
  22. if (now<openingtime | | now>closingtime) {
  23. Request.setattribute ("msg", "registered opening Hours: 9:00-12:00");
  24. Request.getrequestdispatcher ("/msg.jsp"). Forward (request, response);
  25. return false;
  26. }
  27. return true;
  28. }
  29. return true;
  30. }
  31. }


XML configuration:

[HTML]View PlainCopy
  1. <Bean id= "timebasedaccessinterceptor" class=" Com.spring.handler.TimeBasedAccessInterceptor ">
  2. <property name= "openingtime" value="9" />
  3. <property name= "closingtime" value= " /> "
  4. <property name="Mappingurl" value= ". */user\.do\?action=reg.*" />
  5. </Bean>
  6. <Bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
  7. <property name="interceptors">
  8. <list>
  9. <ref bean="Timebasedaccessinterceptor"/>
  10. </list>
  11. </Property>
  12. </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
  1. @Controller
  2. @RequestMapping ("/user.do")
  3. Public class usercontroller{
  4. @Autowired
  5. private UserService UserService;
  6. @RequestMapping (params="Action=reg")
  7. Public Modelandview Reg (Users user) throws Exception {
  8. Userservice.adduser (user);
  9. return new Modelandview ("Profile","user", user);
  10. }
  11. //other option ...
  12. }




You can also configure multiple interceptors, each with a different division of work.

The use of Handlerinterceptoradapter in Spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.