Struts1 is undoubtedly a classic MVC framework. It has the concepts of front-end controllers and business controllers in Struts. So what is the nature of these two controllers? Let me walk you into its world step by step.
Duties of the front-end controller ActionServlet:
1. Intercept the access path
2. instantiate the corresponding service controller based on the intercepted path
Responsibilities of the Business controller Action:
1. Get Parameters
2. Call business logic
3. Complete the redirection
Next, let's take a look at the implementation of a simple struts MVC framework.
Action:
[Html]
Public interface Action {
Public String execute (HttpServletRequest request, HttpServletResponse response) throws Exception;
}
Public class AddUserAction implements Action {
Public String execute (HttpServletRequest request,
HttpServletResponse response) throws Exception {
String username = request. getParameter ("username ");
// Call the business logic
UserManager userManager = new UserManager ();
UserManager. add (username );
Return "/add_success.jsp"; // The redirection path can be read through the configuration file.
}
}
Public interface Action {
Public String execute (HttpServletRequest request, HttpServletResponse response) throws Exception;
}
Public class AddUserAction implements Action {
Public String execute (HttpServletRequest request,
HttpServletResponse response) throws Exception {
String username = request. getParameter ("username ");
// Call the business logic
UserManager userManager = new UserManager ();
UserManager. add (username );
Return "/add_success.jsp"; // The redirection path can be read through the configuration file.
}
}
ActionServlet:
[Html] view plaincopyprint? Public class ActionServlet extends HttpServlet {
@ Override
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Intercept the access URI
String requestURI = request. getRequestURI ();
// Intercept the URI, for example, localhost: 8080 // test/addUser. do.
String path = requestURI. substring (requestURI. indexOf ("/", 1), requestURI. indexOf ("."));
Action action = null;
If ("// addUser". equals (path )){
Action = new AddUserAction ();
} 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 );
}
@ Override
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}
Public class ActionServlet extends HttpServlet {
@ Override
Protected void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Intercept the access URI
String requestURI = request. getRequestURI ();
// Intercept the URI, for example, localhost: 8080 // test/addUser. do.
String path = requestURI. substring (requestURI. indexOf ("/", 1), requestURI. indexOf ("."));
Action action = null;
If ("// addUser". equals (path )){
Action = new AddUserAction ();
} 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 );
}
@ Override
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}
However, there are still some problems, that is, when a business logic is added, We have to modify the ActionServlet, which violates the open/closed principle in the design mode. In the design mode, there is such a sentence that where the if... else... and switch... case... statements exist, reflection can be used to eliminate them.
Struts is through a struts-config.xml to eliminate the if... else... statement to make ActionServlet more universal. Therefore, developers only need to write the Action and configure it in the xml file.
Just calm down and think, isn't it actually a call of a policy mode? Looking back at the systems we have done before, to make the system more flexible, it is actually an application of one policy model after another. The combination of policy and reflection is usually the core component of a framework.
So far, the understanding of the policy mode has to be further explored.
When we first came into contact with the policy model, we understood it as follows: Different practices for one thing. This is a specific thing, for example, payment can be made by credit card, cash, and check. But for payment and dinner, we won't think of this as one thing. In fact, this is only the difference in granularity.
What if it is upgraded to an activity level of a person? Is the two a different solution?
Struts does this. It extracts an interface Action from all requests and modifies the method to execute (). These are the core ideas of Struts, other Struts core objects will be described in the next blog.
The use of the framework will affect the efficiency of the program, but at the current hardware level, these efficiency issues are no longer the main issues, normative development is regarded as the highest program. Software development, from the initial minimum code to the current standard development, has to admire the speed of computer development.