several basic concepts of 1.Struts
1.struts is an open source framework (FRAMESET)
2.struts is a web framework
3.struts is an MVC-based web framework
2. Why is Struts
Because we understand MVC differently, it may cause different companies to write programs, the specification is not uniform, so it is not conducive to the maintenance and expansion of the program and improve the efficiency of development, so we need to use a unified specification to develop the project. So there was struts.
Struts is the application framework of Model-view-controller (MVC) design pattern based on Java EE Web application using Java servlet/jsp Technology, which is a classic product in MVC Classic design pattern.
The advantages and disadvantages of struts are as follows:
Benefits of 1.struts:
Program more standardized
The efficiency of program development has increased
Increased readability of the program
Increased maintainability of the program
Disadvantages of 2.struts:
Form form a little chicken
Action is a single state (affects the processing of web site concurrency)
These are described later in this article.
What we need to know is that the framework, while improving the program's specifications, also constrains the programmer's freedom.
the principle of 3.struts
An example of a user login verification is illustrated.
3.1. Timing Diagram
1.ActionServlet is the total controller, it is struts to provide us, we do not have to write their own, only need to configure. Actionservlet manages the Struts-config.xml file, which is the core file of struts, which is configured with Actionform, the action, and their correspondence, among others.
2.ActionForm is a form for storing data. Development Actionfrom must inherit the Actionform class, which is the norm.
3.Action is a sub-controller, in struts, the action can have more than one. Its essence is the servlet. Develop an action to inherit the action class as well.
4.Model (Java class, service class, EJB, etc.)
3.2.struts Getting Started case
Demonstrated with the above user login verification, and the first struts project was developed using a manual configuration, with the project name Strutslogin.
First, the project directory structure is as follows:
"Steps":
1. When developing struts, a struts development package is required
Struts ' development kit can be downloaded to the official website "" and its latest version is 2.5.1 (June 21, 2016). I downloaded the struts 1.3.10 because I am currently learning the 1.3 version. and add all the jar packages to the Lib folder of the current project.
2. Write login.jsp First
Write the simplest login page login.jsp, as follows:
<%@ page language="Java" Import="Java.util.*"pageencoding="Utf-8"%><! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">"Pragma"Content="No-cache"> <meta http-equiv="Cache-control"Content="No-cache"> <meta http-equiv="Expires"Content="0"> "/strutslogin/login.do"Method="POST"> Username:<input type="Text"Name="username"/><br><br> Password:<input type="Password"Name="Password"/><br><br> <input type="Submit"Value="Login"/> </form> </body>
- Note that for security reasons, login.jsp is placed in the Web-inf directory and forwarded to login.jsp via index.jsp outside the Web-inf directory.
- Note that the action jump address in form is the page under our web app, which is described
login.do later in the Struts-config.xml configuration.
3. Writing Actionform and action
(1) First, you need to write Actionform, which is the form used by the user to fill in the data entered by the user. We named UserForm, which inherits from the Actionform class, as follows:
Public class UserForm extends actionform { PrivateString username;PrivateString password; PublicStringGetUserName() {returnUsername } Public void Setusername(String username) { This. Username = Username; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }}
In fact, this is a very simple javabean, it corresponds to the form in login.jsp, we define attributes in this UserForm username , and password generate their setter and getter methods.
It is important to note that:
<1> general specification: The name of the property should be defined as the name of the control of the JSP page.
<2> But in fact, the name of the property is not necessarily consistent, and the real thing to keep consistent is the setter getter method. That is, if the control name you define in the JSP is called username and password, then the name of the setter method must be setUsername and setPassword , similarly, the name of the getter method must be getUsername getPassword the same as. Properties can be used for other names, but the names of these methods must be fixed:get/set+ the control name and capitalize the first letter
<3> But we are best to abide by the general norms.
<4>. Actionform must be written in accordance with the above specifications, in order to ensure that the data can be filled correctly, to ensure work
(2) Then, write the action, which is the login action, which we named LoginAction , which must inherit from the Action class, as follows:
Public class loginaction extends Action { @Override PublicActionforwardExecute(actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response)throwsException {//Turn the form into the corresponding UserForm objectUserForm UserForm = (UserForm) Form; System.out.println ("User name ="+userform.getusername ()); System.out.println ("Password ="+userform.getpassword ());//Simple verification if("123". Equals (Userform.getpassword ())) {//If the user password is 123, it is legal returnMapping.findforward ("OK"); }Else{returnMapping.findforward ("Error"); } }}
In this class, we need to rewrite a method: excute . This method handles the business logic, which is automatically called, which is somewhat similar to the service method in the servlet, or the Doget/dopost method.
4. Configure the Struts-config.xml file, which is typically placed in the/web-inf directory, which configures the action, Actionform, and their correspondence and jumps
<?xml version= "1.0" encoding= "Utf-8"?><! DOCTYPE struts-config Public "-//apache software foundation//dtd Structs Configuration 1.3//en" "/http Struts.apache.org/dtds/struts-config_1_3.dtd "><struts-config> <!--configuration Forms -- <Form-beans> <!--name is a form name that can be written at will, but the recommended naming specification: Form class names lowercase -- <!--type is used to specify the full path of the form class-- <form-bean name="UserForm" type="Com.gavin.forms.UserForm" ></Form-bean> </Form-beans> <!--Configure Action -- <action-mappings> <!--Configure a specific action-- <!--path represents the resource name for future access to the action, Http://localhost:8080/web/path -- <!--name is used to associate a form -- <!--type is used to specify the full path of the action class-- <action path="/login" name="UserForm" type= "Com.gavin.actions.LoginAction"> <!--here to configure the jump relationship -- <!--name indicates the result names, and path indicates the page address to which it was forwarded- - <forward name="OK" path="/web-inf/welcome.jsp"/> <forward name="error" path="/web-inf/error.jsp"/> </Action> </action-mappings></struts-config>
(1) This struts-config.xml introduces a DTD file: Struts-config_1_3.dtd, this file can be found in the JAR package we downloaded, which defines the constraints of our XML, the root element struts-config .
(2) The configuration file is configured by Form-beans to configure Actionform, and the action is configured through Action-mappings.
(3) We use the Form-bean to configure the specific actionform under Form-beans, such as the current file configuration we wrote UserForm
(4) Use the action under Action-mappings to configure the action, such as the current file configuration we write loginaction
(5) Note the path parameter configuration in action here, which is the address of the form action submitted in our login.jsp, which is written in login.jsp, which login.do login.do will be handed to Struts's master controller Actionservlet to handle, and finally Actionservlet will find the action with the path parameter configured here by a certain mechanism /login , and then find the specific class through the type parameter of the action configuration.
from this process we can find that login.do after the processing of Actionservlet, and finally found the path is /login the action, which seems to have nothing to do with .do this suffix, it is true, this suffix is only a customary usage, We can completely set this suffix to any other name in the Web. xml file.
(6) Finally, the specific action to configure the jump relationship. The parameters configured here excute correspond to the methods written in the Loginaction.
5. Write welcome.jsp and error.jsp pages
Write two simple interfaces.
6. Configuring Actionservlet in Web. xml
Struts Master controller Actionservlet is essentially a servlet, so we'll configure it in Web. XML, as follows:
<servlet> <servlet-name>Action</servlet-name> <servlet-class>Org.apache.struts.action.ActionServlet</servlet-class> <!--Specify the path to profile Struts-config.xml-- <init-param> <param-name>Config</param-name> <param-value>/web-inf/struts-config.xml</param-value> </init-param></servlet><servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>
We want to specify the address of the Struts-config.xml file in this configuration. At the same time, we can see that the suffix we are configuring here is *.do , that is, all .do requests to the end should be given to actionservlet for processing. Here, we also understand why our login.jsp is written in the address of the submission login.do .
4. SummaryBy manually completing a simple struts project, we have a deeper understanding of how struts works and how it works. This process is a little cumbersome, there will be tools generated directly behind, but mastering the underlying principles is an essential step in our learning.
To summarize, in fact, struts is a higher-level refinement of our development projects through Jsp/servlet and using the MVC pattern, which has helped us do most of the work, and we simply need to populate it with the process provided by the struts framework, simplifying the difficulty of our development.
Core knowledge points in struts: the Actionservlet Master Controller, the action sub-controller, the Actionform form, and the configuration of the most important structs-config.xml and Web. xml files.
Struts1.3--struts Getting Started