If you can't simply make it clear, you haven't fully understood it yet. --Einstein
Need things:
1. Working with open source class libraries such as XML document DOM4J
2. Knowledge reserves such as DTD validation
* N to N times? 0 to 1 times + 1 to n times
3. Related knowledge of Java reflection
Reflection process:
Import java.lang.reflect.*
1. Get the Java.lang.Class object for the class you want to manipulate
2. Call class to get the field, method and other objects
Working with the Reflection API
Common classes of reflection
Class: The reflection of the core class, the reflection of all operations are generated around the class, through which you can get the properties of the class, methods and other information
Field class: A property of a class that can get and set property information in a class
Method class: Methods of a class that can obtain information about a method in a class, or execute a method
Constructor class: How to construct a class
1.
The class object is also available through the forname () static method of class, which requires the virtual machine to find and load the specified class
Class CLA = Class.forName ("java.lang.String"); If there is a problem with the string, the ClassNotFoundException exception is thrown
2. Getting information from a class object
Creating objects
Creating an object using the Newinstance () method of the Class object
Notice: In fact, it uses its parameterless construction method to create an instance of the class, so the corresponding class of the class object is required to have a corresponding non-parametric construction method
Text: Build Start
Building a framework based on the MVC pattern
Implementation Scenario 1
All requests are sent to a unified controller (blocking requests with the filter API) so that the Controller is responsible for receiving requests (specifying which action to perform processing according to the requested path)
Then define the action interface, which is used for processing the request, such as login action, register action, etc.
The action invokes model, does the model operation, and returns the corresponding view
//definition of controller class Public classActionfilterImplementsfilter{Privatefilterconfig config; Publicvoic Destroy () {} Public voidDoFilter (servletrequest request, Servletresponse Response,filterchain chain)throwsioexception,servletexception {//Type ConversionsHttpServletRequest HSR =(httpservletrequest) request; Httpserveltresponse HSP=(httpservletresponse) response; //Get ActionAction action = This. Getaction (HSR); //Excute method to invoke actionString Resultview =NULL; Try{Resultview=Action.execute (HSR,HSP); } Catch(Exception e) {e.printstacktrace (); } //page Jump if(NULL!=Resultview) {Request.getrequestdispatcher (Resultview). Forward (request, response); } } Public voidInit (Filterconfig conf)throwsservletexception { This. config =config; } PublicAction getaction (httpservletrequest request) {String URI= Request.getrequesturi ();//gets the requested URLString ContextPath= Request.getcontextpath ();//Get Context PathString Actionpath= Uri.substring (Contextpath.length ());//intercept the part of the context path laterString ActionName= Actionpath.substring (1,actionpath.lastindexof ('. ')) . Trim (); Action Action=NULL; //each subsequent action defined here adds an object to instantiate a desired action class. if("Login". Equals (ActionName)) {Action=Newloginaction (); } returnAction; }}View Code
Definition of the Action interface
Main import javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
....
The invocation execution of the filter-based Actionfilter class needs to be configured in Web. xml
<filter> <filter-name>requestFilter</filter-name> <filter-class> com.xx.xx.mystruts.framework.actionfilter</filter-class></filter><filter-mapping> <filter-name>requestFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
The above is an MVC framework of the main idea of the implementation, it is clear that there are many missing details need to be perfected, but the main body is such.
There are obvious improvements, such as
Using the knowledge of the preceding XML to read the configuration file inject a single action injection through XML
This will not require a new action to add a row if the action class needs to be judged.
Read XML When controller initialization is changed to
This information that is extracted from XML includes the name of the action, the URL of the match, the calling class file, and the path of the view as the result.
Here, a Java bean is used to generate the corresponding object data model after the XML read to read the required information at run time.
Write another manager to manage this action
At the same time the controller needs to be upgraded to load the controller when the Web container starts, and to read the XML we wrote to save the action information,
It is therefore necessary to add the initialization parameters to the filter configuration to implement the function itself
The configuration before modifying Web. XML is similar to the following:
<filter> <filter-name>requestFilter</filter-name> <filter-class> com.xx.mystruts.actionfilter</filter-class> <init-param> <param-name>config </param-name> <param-value>mystruts.xml</param-value> </init-param></filter >
Then is the Actionfilter class code implementation of the modification to implement reading the configuration file
At this time the main use of several things, and they are responsible for the content (about the action mapping map can be easily used HashMap to carry out the corresponding get,put)
The mystruts.xml configuration file, which defines the name resultactionmapping of each action to hold information about the action read from the XML Actionfilter As the controller intercepts the request, initializes the application, allocates the manager of the Actionactionmappingmanager actionmapping it is called to initialize, traverses the XML node, Establish actionmapping based on nodes, and encapsulate some operations on actionmapping, such as ActionManager using reflection technology based on Actionnameget to corresponding actionmapping objects to generate specific Action instance
Attention:
When the above configuration triggers the action when requesting access,
1. Direct access to Localhost:xxx/xx.action
2. Can be called through the JSP page forwarding, when the address can not be xx.action end, but the corresponding JSP page address
JSP page Write
<body> <jsp:forward page= "/index.action" ></jsp:forward></body>
In this case, the filter-mapping in Web. XML is modified as follows:
<filter-mapping> <filter-name>requestFilter</filter-name> <url-pattern>*. action</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>forward</ Dispatcher></filter-mapping>
Readbook: Design your own MVC framework, Java-like STRUTS2 implementations