JavaWeb framework SSH_Struts2 _ (1 ),
1. Getting started with the Struts2 framework and testing the Login demo with the help of intellij idea
1.1 contents of this chapter:
- Getting started with frameworks
2. Details
2.1 getting started with the framework
2.1.1 Struts2 Introduction
(1 ).Struts2 is a lightweight Web development framework based on the MVC model.
MVC mode: The full name of MVC is Model View Controller, short for model-view-controller. It is a software design Model, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic.
- Model: A Model is used to process the data logic of an application. Generally, a Model object is used to access data in a database;
- View: A View is a part of an application that processes data display. Generally, a View is created based on model data;
- Controller: a part of an application that processes user interaction. Generally, the Controller reads data from the view, controls user input, and sends data to the model.
(2 ).Struts2 is centered on WebWork. It uses an interceptor mechanism to process user requests. This design also enables the business logic controller to completely remove from the Servlet API, so Struts2 can be understood as a webwork product.
(3 ).Difference between Struts2 and Struts1:
In terms of technology, Stuts1 has a core controller, but only provides one interface, that is, execute, and configures actionform and so on. Therefore, the dependency is strong, while Struts2 is based on webwork, the so-called AOP idea developed for Interceptor can be configured with multiple actions, which is convenient to use. However, because the interceptor before the request has some injection operations, the speed is a little slower than that of Struts1.
(4) Advantages of Struts2:
- Open-source projects, easy to use and expand
- Exception Handling Mechanism
- Result-based page navigation, using the Result tag to easily achieve redirection and page navigation
- Provides powerful labels that can reduce page code
- Provides good Ajax support
2.1.2 getting started with Struts2
I use intellij idea to write a simple case. It is hoped that this simple case will help you get a preliminary understanding of the running process of struts2. Of course, there are also configuration operations related to IntelliJ idea. I am using the ubuntu system. I will not talk about idea installation, JDK configuration, tomcat configuration, and so on. There are many related tutorials. I just need to find them by myself.
Download the latest version of struts2. we recommend that you download struts-xxx-min-lib.zip. After the download is complete, the lib directory should contain the following jar package:
(2) Open intellij idea and create a new Project.
(2) Select Java javasises ---> Web Application (...) ---> Struts2 (...) ---> Use Library
(3) Select use library instead of Download. Because the Struts2 versions configured by idea are not necessarily the same, we directly select the jar in Struts2; select Create --> (find the jar package path in step (1) and press Ctrl to select all.
(4) next ----> after the name is completed (I name it Struts2_TestDemo_Project), the project bar is as follows:
(5) completed projects include Struts. xml, web. xml, and index. jsp. Next, perform simple configuration;
- Open File ---> Project Structure ---> Problem ---> if any Problem occurs, click fix and select add Struts2 to the artifact ---> OK;
- Modify the content of filter-class in web. xmlOrg. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
- Move the cursorStrutsPrepareAndExecuteFilterThe following message is displayed:
(Delete "ng." In the row directly at this time. No prompt will be displayed after completion)
(6) The basic configuration is completed here. Next, write a logon Demo;
- In the back-end section, create a java package (test_login) in src ----> set up a java class (LoginAction) Code as follows:
LoginAction:
package test_login;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport { private String username; private String password; @Override public String execute() throws Exception { if (username.equals("admin") && password.equals("123")) { return SUCCESS; } else { return LOGIN; } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
- The Struts. xml configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"><struts> <package name="default" namespace="/" extends="struts-default"> <action name="Login" class="test_login.LoginAction"> <result name="success">/success.jsp</result> <result name="login">/index.jsp</result> </action> </package></struts>
<% -- Created by IntelliJ IDEA. User: mairr Date: 17-11-17 Time: To change this template use File | Settings | File Templates. -- %>
<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%>
- Log on to the success. jsp page.
<% -- Created by IntelliJ IDEA. user: mairr Date: 17-11-17 Time: PM To change this template use File | Settings | File Templates. -- %> <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%>
- After the Project is released, File ---> Project Structure ---> Artifacts ---> Add the struts2 Project to OutputLayer ---> OK ----> Run ---> index. jsp, open the google browser (http: // localhost: 8080/index. jsp), the login interface is as follows:
- Enter the username admin and password 123. The browser interface is as follows:
2.1.1 Struts2 Execution Process Analysis
The execution flowchart of Struts2 is as follows:
(1) The client browser sends a request (HttpServletRequest );
(2) The program will call StrutsPrepareAndExecuteFilter and then ask whether the ActionMapper request needs to call an Action;
(3) If ActionMapper decides to call an Action, StrutsPrepareAndExecuteFilter will process the request to ActionProxy (Action proxy );
(4) ActionProxy reads the framework Configuration information from the Configuration file (Struts. xml) through the Configuration Manager to find the Action class to be called;
(5) ActionProxy creates an ActionInvocation instance;
(6) ActionInvocation uses the naming mode to call the Action. before calling the Action, all configured interceptors (Intercepter1, Intercepter2, and Intercepter3.) are called in sequence ..);
(7) once the Action is executed, ActionInvocation returns the Result string to search for the Result corresponding to the Result string and then executes the Result. Generally, results call some templates (such as JSP) to present pages.
(8) The Result information is returned to ActionInvocation. In this process, the interceptor will be executed again (the order is opposite to that before the Action is executed );
(9) generate an HTTP servletresponse Response Behavior and report it to the client through StructsPrepareAndExecuteFilter;
Reference books: SSH framework integration tutorial
Reference: http://blog.csdn.net/rcnjtech/article/details/77914381