Struts2 framework details

Source: Internet
Author: User

Struts2 framework details

The Model-View-Controller mode of Struts2 implements the following five core components:

 

Action-Actions

Interceptor-Interceptors

Value stack/OGNL

Result/result type

View Technology

 

Struts 2 is somewhat different from the traditional MVC framework. The role model in the framework is slightly different than the controller, although there are some overlapping. This is an overview of Struts's MVC Architecture.

 

Describes Struts2's high-level architecture model, view, and controller. A Struts2 scheduling Servlet filter, interceptor and controller implementation, the result type and result of the combination of the action and view implemented by this model. The value stack and OGNL provide commonalities for connecting and integrating other components.

In addition to the above components, there will be a specific information that involves configuration. Configure the Web application, and configure the action, interceptor, and result.

 

Based on the above data graph, we can explain that the lifecycle of a user's request is as follows in Struts 2:

Hello World, Hello World From Struts2
  1. User requests are sent to the server to request certain resources (that is, pages ).

    This filter schedules requirements and determines the appropriate actions.

    The interceptor configuration function is applicable to verification and file upload.

    Select the action to execute the requested action.

    Similarly, configure the interceptor for any post-processing, if necessary.

    The final result is prepared by the view and returned to the user.

    1. After downloading and decompressing struts, copy the following files under the lib directory to our project.WEB-INFlibDirectory. To do this, you can simply drop all the following files in the WEB-INFlib folder. (Below is the minimum dependency set)

       

      Commons-fileupload-x.y.z.jar

      Commons-io-x.y.z.jar

      Commons-lang-x.y.jar

      Commons-logging-x.y.z.jar

      Commons-logging-api-x.y.jar

      Freemarker-x.y.z.jar

      Using sist-. xy. z. GA

      Ognl-x.y.z.jar

      Struts2-core-x.y.z.jar

      Xwork-core.x.y.z.jar


       

       

      Create Action Class ):
      • Action class is the key to Struts 2 applications. We have implemented the business logic in most Action classes. Action class to respond to user operations, when the user clicks a URL. One or more methods in the Action class are executed and a string result is returned. Displays a specific JSP page based on the result value.

         

         

        package com.tutorialspoint.struts2;public class HelloWorldAction{   private String name;   public String execute() throws Exception {      return success;   }      public String getName() {      return name;   }   public void setName(String name) {      this.name = name;   }}

        The HelloWorldAction class of the Struts2 framework creates an object and calls the execute method to respond to user operations. Execute the execute method in the business logic and return a String constant. Simply put, you must execute an operation class for each URL, or you can directly use the name of this class as the operation name, or map to some other names using struts. xml file, as shown in.

         

         

        Create View)

         

        We need a JSP to submit the final message. This page uses the pre-defined action in the Struts 2 framework, which will occur in the struts ing defined in the Struts. xml file.

        <%@ page contentType=text/html; charset=UTF-8 %><%@ taglib prefix=s uri=/struts-tags %>

        The taglib Command tells the Servlet container that the page will use the Struts2 labels, which will be passed through s before. S: The property tag displays the property value of the handler class 'name'. It is the getName () method that calls the HelloWorldAction class ().

        Next, create a home page:

        We also need to create the index. jsp page file in the WebContent folder. This file is used as the initial action URL. You can directly click the HelloWorldAction class definition method called by the Struts2 framework and render the HelloWorld. jsp view.

        There may be a problem:

         

        The web project cannot be deployed to the local tomcat configured in eclipse.

        I. Problems Found

        Create a Dynamic Web Project in eclipse, configure the local tomcat, write the code, and select Run on Server, however, after running the program, the created Project name does not appear in the webapps under the tomcat installation directory.

        Ii. Verification

        Obviously, the project is not automatically deployed in Tomcat's Webapps, but in other containers. Enter http: // localhost: 8080/webDemo/login. jsp in the built-in browser. However, when http: // localhost: 8080 is opened in an external browser, the expected Kitten Screen does not appear.

        Iii. Cause

        Eclipse does not deploy the project to webapps under the tomcat installation directory by default in MyEclipse, but is deployed to the working directory by default.

        In. metadata. pluginsorg. eclipse. wst. server. core mp1wtpwebapps, The tmp1 folder stores the project information.

        4. Modify

        To deploy the project to webapps under the tomcat installation directory by default, show view> servers> Find the tomcat to be modified> right-click ① To stop the Tomcat server (stop) in eclipse) ② add and remove items deployed in the container ③ clear the container related data (clean) ④ open the tomcat modification interface (open)

        ⑤ Locate the servers location and select the second (User tomcat Installation) ⑥ modify the deploy path to webapps 7 and save and close

        It must be noted that ① ② must be operated; otherwise, the following steps will be grayed out and cannot be operated.


         

        Another error occurs at The beginning of jsp: JavaWeb: the superclass javax. servlet. http. HttpServlet was not found on The Java Build Path

         

        In the original Java Web project, no Tomcat runtime classes were added. The following are specific solutions: 1. Right-click the web Project-> attribute or Build Path-> Java Build Path-> Libraries-> Add Libray... -> Server Runtime-> Tomcat Server 2. Switch to Orader and Export on the Java Build Path interface and select Tomcat.

         

        The Code on the home page is as follows (index. jsp ):

         

        <%@ page language=java contentType=text/html; charset=ISO-8859-1   pageEncoding=ISO-8859-1%><%@ taglib prefix=s uri=/struts-tags%>   

        The hello action definition in the preceding view file will be mapped to the HelloWorldAction class and its execution method using the struts. xml file. When you click the "Submit" button, it will cause the Struts2 framework to run the execution method definition in it, based on the return values of the HelloWorldAction class and method, select and present a response for the corresponding view.

         

         

        Configuration File

         

        We need a ing to work with URL, HelloWorldAction class (model), and HelloWorld. jsp (view ). Ing tells Struts 2 how the framework class will respond to user operations (URLs). This type of method will be executed and the View selected based on the method returned to display the string results.

         

               
               
                   
                                        
                  
                   /Hello.jsp
                           
                 
                
               

        Here, we set struts. devMode to true, because we are working on the development environment, we need to see some useful log messages. Then, we define a package called helloworld. It is useful to create a package when you want to put the classes in it. In our example, our action is named "hello". The corresponding URL is/hello. action and the backup is HelloWorldAction. class. HelloWorldAction. class.ExecuteThe method is the URL method/hello. action that is run at the time of the call. If the execute method returns success, we will introduce the user to HelloWorld. jsp.

         

        The next step is to createWeb. xmlFile. The entry point of the Struts2 application will be a filter defined in the deployment descriptor (web. xml.

        In the web. xm file, we define a filter org. apache. struts2.dispatcher. FilterDispatcher. web. xml file that requires creating WebContent under the WEB-INF folder. Eclipse has created a web. xml file when creating a project. Therefore, we only need to modify it as follows:

               
                     
                
                 Struts 2
                   
                      
                 
                  index.jsp
                    
                   
                      
                 
                  struts2
                       
                 
                           org.apache.struts2.dispatcher.FilterDispatcher      
                    
                   
                      
                 
                  struts2
                       
                 
                  /*
                    
                
               

        We have specified index. jsp as the file on our homepage. We have configured all the URLs running on the Struts2 filter (I. e, any URL that matches the pattern /*)

         

        Enable detailed logs:

         

        You can enable the complete logging function, and create both by creating a job and Struts2Logging. propertiesFile inWEB-INF/classesFile. In the property file, note the following two lines:

         

        org.apache.catalina.core.ContainerBase.[Catalina].level = INFOorg.apache.catalina.core.ContainerBase.[Catalina].handlers =                               java.util.logging.ConsoleHandler

        By default, logging. properties specifies the ingress record of ConsoleHandler to stdout and FileHandler in the same way. You can set the Log threshold of a processing program to SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, or ALL.
        That's it. We are ready to run our Hello World application using the Struts 2 framework.

         

        The following are a few important information to be aware of. The most important thing is to check the error information and find the cause after an error occurs. The problem is due to two reasons:

        1) writing errors in the configuration file.

        2) the import package is incomplete. Just follow the error message to export the package.

         



         

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.