Struts -- Overview

Source: Internet
Author: User

What is struts?

Struts is a framework.

In the previous design, the client browser directly redirects or forwards requests to the page through the container call class, while Struts provides a variety of tools to decouple the class from the interface.

The purpose of Struts is to help us reduce the time needed to develop Web applications using MVC design models. Struts is a good choice if we want to use the advantages of servlets and JSP together to build scalable applications.

How does sturts work?

How struts works, such:


The client browser sends a request through the configuration file to call the actionservlet class (has been written to meet the basic needs), through the name we can determine that it is a servlet; this class reads the struts configuration file Struts-config.xml; implement actionform assignment (the assignment principle is skipped). Locate the action class that really works in the call according to the Action-mapping in the configuration file. In struts, the action class calls the business logic.

 

Struts workflow:


When the web application starts, it loads, initializes actionservlet, actionservlet reads configuration information from the struts-config.xml file, stores them in various configuration objects, when actionservlet receives a customer request, the following process will be executed.

(1) retrieve the actionmapping instance that matches the user request. If it does not exist, the system returns invalid information about the request path;

(2) If the actionform instance does not exist, create an actionform object and save the form data submitted by the customer to the actionform object;

(3) determine whether form verification is required based on the configuration information. If verification is required, call the validate () method of actionform;

(4) If the validate () method of actionform returns NULL or an actuiberrors object that does not contain actionmessage is returned, the form verification is successful;

(5) actionservlet determines the action to which the request is forwarded based on the actioning information contained in actionmapping. If the corresponding action instance does not exist, create the instance first, and then call the action's execute () method;

(6) The execute () method of action returns an actionforward object. The actionservlet is forwarding the client request to the JSP component pointed to by the actionforward object;

(7) The actionforward object directs to the JSP component to generate a dynamic webpage and return it to the customer;

 

How to Use struts?

 

The Web. xml configuration file is as follows:

<servlet>   <servlet-name>action</servlet-name>   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <init-param>     <param-name>config</param-name>     <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <init-param>     <param-name>debug</param-name>      <param-value>2</param-value>    </init-param>    <init-param>     <param-name>detail</param-name>      <param-value>2</param-value>    </init-param>   <load-on-startup>2</load-on-startup>  </servlet>  <servlet-mapping>   <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping> 


 

Strutsconfig. xml configuration file:

<struts-config><form-beans><form-beanname="loginForm" type="struts.LoginActionForm"/></form-beans> <action-mappings><actionpath="/login"type="struts.LoginAction"name="loginForm"                scope="request"                ><forwardname="success" path="/login_success.jsp" /><forwardname="error" path="/login.jsp"/></action></action-mappings></struts-config>


 

Configure actionform under the label <form-beans> and Action-mapping.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.