Implementation features
The code above implements the corresponding execution method through the path, but the parameters of the execution method are the parameters that must be answered after the incoming request.
the following were: HttpServletRequest and httpservletresponse. The parameters and processing parameters are then obtained from the two objects.
Requirements: We want to determine whether Servletapi objects are required based on the parameter list of the method declaration
- HttpServletRequest
- HttpServletResponse
- HttpSession
- ServletContext
Rather than fixed must httpservletrequest and HttpServletResponse.
Implementation Ideas
A list of parameters for the execution method is obtained through reflection technology. Determine the Servletapi object that needs to be passed in.
implementation Steps
1. In The Controllerrelolver class adds a parameter list method for judging the execution method getparameterobjects
1 Privateobject[] getparameterobjects (parameter[] parameters) {2 //1. Create an array that sets a parameter object, and the format of the parameter is consistent with the parameter type3object[] Parameterobjects =NewObject[parameters.length];4 //2. Depending on the type of the parameter, the object is filled in an array of objects5 for(inti=0;i<parameters.length;i++){6Class<?> Typeclass =Parameters[i].gettype ();7 //3. Depending on the type of method parameter and whether the 4 roles we have established are parent-child relationships. If so, assign a value8 if(ServletRequest.class. IsAssignableFrom (Typeclass)) {9parameterobjects[i]= This. Request;Ten}Else if(Servletresponse.class. IsAssignableFrom (Typeclass)) { Oneparameterobjects[i]= This. Response; A}Else if(HttpSession.class. IsAssignableFrom (Typeclass)) { -parameterobjects[i]= This. session; -}Else if(ServletContext.class. IsAssignableFrom (Typeclass)) { theparameterobjects[i]= This. Application; - } - - } + returnparameterobjects; -}
2. Modify the Execute () method of the Controllerrelolver and call the Getparameterobjects method to pass in the corresponding value when invoking the controller's method dynamically, based on the parameter list of the executing method
1 PublicString Execute (httpservletrequest request,httpservletresponse response,context Context)throwsillegalaccessexception, illegalargumentexception, invocationtargetexception{2 //1. Set the API objects required by the servlet3 This. request=request;4 This. response=response;5 This. session=request.getsession ();6 This. application=Request.getservletcontext ();7 8 //1. Get the requested path9String URI =Request.getrequesturi ();Ten //2. Obtain the mapping path according to the path rule OneString Path = This. Pathrule (URI); A //3. Obtain the object and execution method of the corresponding business controller in the container through the path -Mappingenttiy mappingenttiy = This. Getrequestmethod (path, context); - //4. Obtaining the method of execution theMethod method =Mappingenttiy.getmethod (); - //5. Get the business controller for the path -Object Controller =Mappingenttiy.getcontroller (); - //6. Execution method, must have request,response two parameters of execution method +parameter[] Parameters =method.getparameters ();20//7. To the execution method, set the corresponding Servletapi object according to the parameters of the execution method object[] objects = this.getparameterobjects (parameters); 2 2//8. Dynamic call method, the object corresponding to the assignment of the value of the method of execution, Object resultobject = Method.invoke (Controller, objects); - if(resultobject!=NULL){ - //7. Returns the mapping string returned by the execution method - return(String) resultobject; - } - return NULL; in}
Test Code
1. Test the directory structure of your Web project
2. Modify the execution method of the test Usercontroller, there are multiple SERVLETAPI parameters
1 /**2 * Path rule:3 * 1. Must include/start4 * 2. Suffix must be ignored and not written5 * such as:http://localhost: 8080/webmvc-demo-test-01/test.do6 * The corresponding mapping path is:/test7 * 8 */9@RequestMapping (value = "/login")Ten PublicString Login (httpservletrequest request,httpservletresponse response,servletcontext application,httpsession session) { OneSYSTEM.OUT.PRINTLN ("-Login controller-")); A System.out.println (request); - System.out.println (response); - System.out.println (application); the System.out.println (session); - return"Redirect:/login.jsp"; - -}
3. Test the result, output the corresponding object. Success!
Write frames together-MVC framework-dynamic binding of basic function-servletapi (v)