Since I first came into contact with Java, I often see some experts on the Forum talking about the framework cloud. Every one of them feels that what they know about the framework is really great. Envy, envy, and hate cannot be used as food. We also follow the giant's pace to start the study framework journey. Struts is the simplest and most classic of all frameworks. Starting with Struts, it is a step-by-step shortcut.
This article serves as a preparation article. Let's start with the destruction of the origin-the basic principles of MVC.
The functional collaboration process between the three parts of the MVC framework is clear, but here you can briefly mention:
1. the end user sends a request to the Controller.
2. After the Controller accepts the request, it checks the request and determines what business logic is used to process the request. In this case, the Controller forwards the request to a corresponding business logic for processing;
3. The model contains all the business components that process the user's request, and also executes all the data access required by the user, any data retrieved by end users is packaged and returned to the Controller.
4. The controller accepts the data returned from the model, displays the corresponding view of the data, and returns the view to the user.
Below, we will first use an inflexible servlet for implementation:
Package COM. xxjstgb. servlet; import Java. io. ioexception; import Java. util. list; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class testservlet extends httpservlet {@ overrideprotected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string requesturi = request. getrequesturi (); int start = requesturi. indexof ("/", 1); int end = requesturi. indexof (". "); string Path = requesturi. substring (START, end); system. out. println ("Path =" + path); string username = request. getparameter ("username"); usermanager = new usermanager (); string forward = ""; if ("/servlet/deluser ". equals (PATH) {usermanager. del (username); Forward = "/del_success.jsp";} else if ("/servlet/adduser ". equals (PATH) {usermanager. add (username); Forward = "/add_success.jsp";} else if ("/servlet/modifyuser ". equals (PATH) {usermanager. modify (username); Forward = "/modify_success.jsp";} else if ("/servlet/queryuser ". equals (PATH) {list userlist = usermanager. query (username); Request. setattribute ("userlist", userlist); Forward = "/query_success.jsp";} else {Throw new runtimeexception ("request failed");} request. getrequestdispatcher (forward ). forward (request, response );}}
Configuration file:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>TestServlet</servlet-name><servlet-class>com.xxjstgb.servlet.testServlet</servlet-class></servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>
When a user sends a request (for example, the request is http: // localhost: 8080/struts_dispatchaction/servlet/deluser. do), according to the user request URL with certain characteristics (this URL starts with/servlet/or *. end with Do) to access the corresponding servlet.
According to the configuration file, all requests configured with the end of ". Do" can access testservlet. The servlet intercepts the special part of each request "/servlet/deluser". Based on this string, it calls the corresponding business logic and performs the corresponding redirection.
However, this implementation method may seem inappropriate at a Glance. The servlet task is too heavy. The model layer logic is called here, and all page turns are also performed. In addition, it contains a large number of if... Else statements, due to service instability, it is a very bad design to add or delete else statements.
In the following improved design, We refined the controller into the frontend controller testservlet and the Business controller action.
The front-end controller has two tasks: 1. Extract the URL according to certain rules; 2. Distribute the request to the corresponding action according to the URL.
Business Controller: 1. Accept form data; 2. Call business logic; 3. Return the redirection information.
Action:
package com.xxjstgb.servlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public interface Action {public String execute(HttpServletRequest request, HttpServletResponse response)throws Exception;}
Deluseractiion:
Package COM. xxjstgb. servlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class deluseraction implements action {Public String execute (httpservletrequest request, httpservletresponse response) throws exception {// receives the form parameter string username = request. getparameter ("username"); // call the business logic usermanager = new usermanager (); try {usermanager. del (username);} catch (exception e) {return "del_error.jsp"; // The redirection path can be read through the configuration file} // The redirection information return "/del_success.jsp "; // The conversion path can be read through the configuration file }}
Testservlet after improved design:
Package COM. xxjstgb. servlet; import Java. io. ioexception; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class testservlet extends httpservlet {@ overrideprotected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string requesturi = request. getrequesturi (); int start = requesturi. indexof ("/", 1); int end = requesturi. indexof (". "); string Path = requesturi. substring (START, end); Action action = NULL; If ("/servlet/deluser ". equals (PATH) {Action = new deluseraction ();} else if ("/servlet/adduser ". equals (PATH) {Action = new adduseraction ();} else if ("/servlet/modifyuser ". equals (PATH) {Action = new modifyuseraction ();} else if ("/servlet/queryuser ". equals (PATH) {Action = new queryuseraction ();} else {Throw new runtimeexception ("request failed");} string forward = NULL; try {forward = action.exe cute (request, response);} catch (exception e) {e. printstacktrace ();} request. getrequestdispatcher (forward ). forward (request, response );}}
In this servlet, the call and page redirection of the model layer have been separated into the action. In a specific action, the model layer logic and page redirection are called. This is the simple Implementation of the Basic MVC framework.
The sequence diagram is as follows:
However, the main problem is still unsolved. There are still a large number of if... The else structure. During the modification, business changes still need to be removed here.
Therefore, we can configure the corresponding action in the configuration file and remove if… through reflection... Else. This is the Struts framework. Later.