Two ways to obtain the URL and controller mapping relationship in SPRINGMVC (736c677c4 available)

Source: Internet
Author: User
Package com.jeeplus.modules.dynamictrace.web;
Import Com.jeeplus.core.web.BaseController;
Import Org.springframework.beans.factory.BeanFactoryUtils;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.context.WebApplicationContext;
Import Org.springframework.web.context.support.WebApplicationContextUtils;
Import Org.springframework.web.method.HandlerMethod;
Import org.springframework.web.servlet.HandlerMapping;
Import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
Import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
Import Org.springframework.web.servlet.mvc.method.RequestMappingInfo;

Import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
Import Javax.servlet.ServletContext;
Import Javax.servlet.http.HttpServletRequest; Import Java.util.ArraYlist;
Import Java.util.Iterator;
Import java.util.List;

Import Java.util.Map; /** * Mayor's mailbox Controller * @author SLG * @version 2017-10-02/@Controller @RequestMapping (value = "${adminpath}/dynamict Race ") public class Urlmappingcontroller extends Basecontroller {/************************************** First method (more complex) * /Private class Requesttomethoditem {public String Controllern
        Ame
        Public String methodname;
        Public String RequestType;
        Public String Requesturl;

        Public class<?>[] Methodparmatypes;
                                   Public Requesttomethoditem (String requesturl, String RequestType, String controllername, String requestmethodname,
            Class<?>[] methodparmatypes) {this.requesturl = Requesturl;
            This.requesttype = RequestType;
            This.controllername = Controllername;
           This.methodname = Requestmethodname; This.methodparmatypes = Methodparmatypes; }} @RequestMapping (value = "/temp", method = requestmethod.get) Public String index (HttpServletRequest reque
        ST) {ServletContext ServletContext = Request.getsession (). Getservletcontext ();
        if (ServletContext = = null) {return null;

        } webapplicationcontext Appcontext = Webapplicationcontextutils.getwebapplicationcontext (ServletContext); Mapping of request URLs and processing methods list<requesttomethoditem> requesttomethoditemlist = new ARRAYLIST&LT;REQUESTTOMETHODITEM&G
        t; (); Get all the requestmapping map<string, handlermapping> allrequestmappings = beanfactoryutils.beansoftypeincluding

        Ancestors (Appcontext, Handlermapping.class, True, false); For (handlermapping handlerMapping:allRequestMappings.values ()) {//This project only needs Requestmappinghandlermappi The URL mapping in ng if (handlermapping instanceof Requestmappinghandlermapping) {requestmappinghandlermapping requestmappinghandlermapping = (requestmappinghandle
                rmapping) handlermapping;
                Map<requestmappinginfo, handlermethod> handlermethods = Requestmappinghandlermapping.gethandlermethods (); For (Map.entry<requestmappinginfo, handlermethod> requestMappingInfoHandlerMethodEntry:handlerMethods.entry Set ()) {Requestmappinginfo Requestmappinginfo = requestmappinginfohandlermethodentry.
                    Getkey ();

                    Handlermethod Mappinginfovalue = Requestmappinginfohandlermethodentry.getvalue ();
                    Requestmethodsrequestcondition methodcondition = Requestmappinginfo.getmethodscondition ();
                    Patternsrequestcondition patternscondition = Requestmappinginfo.getpatternscondition ();
                    String requesttype= "";
                    String Requesturl = ""; for (Requestmethod M:methodconditIon.getmethods ()) {requesttype=m.name ();
                    For (String s:patternscondition.getpatterns ()) {requesturl=s;
                    String controllername = Mappinginfovalue.getbeantype (). toString ();
                    String requestmethodname = Mappinginfovalue.getmethod (). GetName ();
                    class<?>[] Methodparamtypes = Mappinginfovalue.getmethod (). Getparametertypes ();
                            Requesttomethoditem item = new Requesttomethoditem (Requesturl, RequestType, Controllername, Requestmethodname,

                    Methodparamtypes);
                Requesttomethoditemlist.add (item);
            } break;
    } return ""; /************************************* the second method (relatively simple) ************************************************/public Webapplicationcontext Getwebapplicationcontext (ServletContext SC) {
        Return Webapplicationcontextutils.getrequiredwebapplicationcontext (SC); @RequestMapping (value = "/geturlmapping", method = requestmethod.get) public String geturlmapping (httpservletre
        Quest request) {Webapplicationcontext WC = Getwebapplicationcontext (Request.getsession (). Getservletcontext ());
        Requestmappinghandlermapping RMHP = Wc.getbean (Requestmappinghandlermapping.class);
        Map<requestmappinginfo, handlermethod> map = Rmhp.gethandlermethods (); for (iterator<requestmappinginfo> iterator = Map.keyset (). iterator (); iterator. Hasnext ();)
            {Requestmappinginfo info = Iterator.next ();

            Handlermethod method = Map.get (info);
            System.out.println (Method.getmethod (). Getdeclaringclass (). toString () + "--");
            System.out.println ("Consumescondition" +info.getconsumescondition ());
            System.out.println ("Headerscondition" +info.getheaderscondition ()); SysteM.out.println ("Methodscondition" +info.getmethodscondition ());
            System.out.println ("Paramscondition" +info.getparamscondition ());
            System.out.println ("Patternscondition" +info.getpatternscondition ());
            System.out.println ("Producescondition" +info.getproducescondition ());

            System.out.println ("============================");
        System.out.print (Method.getmethodannotation (Requestmapping.class). params () [0]);
    Return ""; }




}

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.