Readbook: self-designed mvc Framework. java is similar to the implementation of struts2, mvcstruts2
If you cannot make it clear, you have not fully understood it yet. -- Einstein
Need things:
1. operate open-source libraries such as the xml document dom4j
2. Knowledge reserves such as dtd Verification
* N to n times? 0 to 1 + 1 to n times
3. java reflection knowledge
Reflection process:
Import java. lang. reflect .*
1. Obtain the Java. lang. Class Object of the Class to be operated
2. Call the Class Method to obtain objects such as Field and Method.
Use the reflection api for operations
Common Reflection classes
Class: the Core Class of reflection. All reflection operations are generated around this Class. Through this Class, you can obtain attributes, methods, and other information of the Class.
Field Class: class attributes. You can obtain and set attributes in the class.
Method class: class Method. You can obtain information about methods in the class or execute methods.
Constructor class: Constructor class
1.
You can use the forName () Static Method of the Class to obtain the Class object corresponding to the Class. It requires the virtual machine to find and load the specified Class.
Class linoleic = Class. forName ("java. lang. String"); If the String is faulty, A ClassNotFoundException exception is thrown.
2. Get information from the Class Object
Create object
Use the newInstance () method of the Class Object to create an object
Notice: Actually, it uses its non-parametric constructor to create an instance of this Class. Therefore, the corresponding Class of this Class object must have a corresponding non-parametric constructor.
Body: build start
Construct a framework based on the mvc pattern
Solution 1
All requests are sent to a unified controller (using the filter api to intercept requests). In this way, the controller is responsible for receiving requests (which action is used to specify the Request Path for processing ).
Then define the Action interface for request processing, such as login action and registration action.
Action: Call model to perform model operations and return the corresponding view.
// Controller class definition public class ActionFilter implements Filter {private FilterConfig config; public voic destroy () {} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {// type conversion HttpServletRequest hsr = (HttpServletRequest) request; HttpServeltResponse Mk = (HttpServletResponse) response; // obtain action Action action = this. getActi On (hsr); // call the excute method of action String resultView = null; try {resultView = action.exe cute (hsr, MK);} catch (Exception e) {e. printStackTrace ();} // page Jump if (null! = ResultView) {request. getRequestDispatcher (resultView ). forward (request, response) ;}} public void init (FilterConfig conf) throws ServletException {this. config = config;} public Action getAction (HttpServletRequest request) {String uri = request. getRequestURI (); // obtain the request url String contextPath = request. getContextPath (); // obtain the context path String actionPath = uri. substring (contextPath. length (); // part of the String actionName = actionPath after the context path is intercepted. substring (1, actionPath. lastIndexOf ('. ')). trim (); Action action = null; // Add an object for instantiating a required Action class if ("login ". equals (actionName) {action = new LoginAction () ;}return action ;}}View Code
Action Interface Definition
Mainly import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
....
The call 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 the main implementation of a general idea of the mvc framework. It is obvious that there are still many missing details that need to be improved, but the main body is like this.
There are obvious improvements, such
Read the configuration file with the xml knowledge before and inject one action into xml.
In this way, you do not need to add a new action to add a line if to judge the action class required for processing.
Read the xml when the controller is initialized.
The information defined in xml includes the action name, matched url, and the called class file, which serves as the path of the result view.
Here, a java Bean is used to generate the corresponding object data model after xml reading to read the required information at runtime.
Write another manager to manage this action.
At the same time, the controller also needs to be upgraded so that the web Container can load the controller when it starts and read the xml that we wrote to save the action information,
Therefore, you must add initialization parameters in the filter configuration to implement this function.
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, the actionFilter code is modified to read the configuration file.
At this time, the main things used and the content they are responsible for respectively are (the action ing of action mapping can be used easily for corresponding get and put)
Mystruts. the xml configuration file defines the name of each Action. resultActionMapping is used to save the information of the Action read from the xml. ActionFilter is used as the controller to intercept requests and initialize applications, the manager that assigns actionActionMappingManager ActionMapping. When it is called for initialization, it traverses the xml node, creates ActionMapping Based on the node, and encapsulates some operations on ActionMapping, for example, ActionManager uses reflection technology to generate a specific Action instance based on ActionNameget to the corresponding ActionMapping object.
Note:
The above configuration triggers the action when requesting access,
1. You can directly access localhost: xxx/xx. action
2. It can be called through jsp page forwarding. In this case, the address does not end with xx. action, but the corresponding jsp page address.
Write in jsp page
<body> <jsp:forward page="/index.action"></jsp:forward></body>
In this case, 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>