Struts1 operating principle and integration steps, and struts1 principle Integration

Source: Internet
Author: User

Struts1 operating principle and integration steps, and struts1 principle Integration

Struts1

Struts1 running procedure

1. Project initialization: load the web when the project starts. xml, struts1's total controller ActionServlet is a Servlet, which is in the web. xml is configured to automatically start the Servlet, at startup the Controller will read the configuration information of the configuration file (struts-config.xml), initialize the corresponding objects for different modules in struts.

2. Send a request: the request is sent by the central controller of ActionServlet (on the web. after receiving the configuration in xml, the configuration file (srtuts-config) will be read to find the Action object corresponding to the request.

3. Request Parameters: the struts controller ActionServlet puts the data in the corresponding ActionForm object when the user submits the request, actionForm automatically receives form data based on the name = "" configured in the configuration file.

4. Request distribution: the Controller distributes requests to specific actions based on the configuration information object ActionConfig, and the corresponding FormBean is sent to the excute () method in the Action.

5. Business Processing: Action generally only contains one excute () method. It is responsible for executing the corresponding business logic (calling other business modules) and returning an ActionForward object. The server forwards data through the ActionForward object.

6. Return response: Action returns a target response object to the total controller for different results of business processing.

7. Search for response: the total controller processes the target response object returned by the business based on the Action and finds the corresponding resource object, which is generally a jsp page.

8. Response User: The target response object passes the result to the resource object and displays the result to the user.

 

Conclusion: the client sends the request. do and distributes it to the corresponding action for processing. Several parameters need to be passed during processing: request, response, and mapping (encapsulate the configuration into an object ), there is also an actionForm (automatically receives form data based on the name = "" configured in the configuration file, and finally calls the business logic and returns some data, the returned ActionServlet is an actionForward jump information, through mapping. findForward is found, and then the servlet will automatically challenge you to the corresponding page.

 

Integration steps:(Import the corresponding jar package)

Step 1: Configure web. xml

    <servlet>      <servlet-name>action</servlet-name>      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   </servlet> <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern> </servlet-mapping>

Step 2: Configure struts-config.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts-config PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 1.3 // EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings>
<! -- Path the Action object corresponding to the path welcome. do type request requested by the customer --> <action path = "/welcome" type = "com. lwl. Action. WelcomeAction">
<Forward name = "success" path = "/WEB-INF/page/welcome. jsp"> </forward>
</Action>
           <init-param>              <param-name>config</param-name>              <param-value>/WEB-INF/struts-config.xml</param-value>            </init-param>
</action-mappings> </struts-config>

Step 3: Compile the Action object

public class WelcomeAction extends ActionServlet {
    public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) return mapping.findForward("success");
}

Step 4: Compile welcome. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Step 5: Test

Access address: localhost: 8080/project name/welcome. do

Jump to welcome. jsp output:

Hello, struts1 is running successfully.

Test successful

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.