Build a Struts2 development environment and complete a HelloWorld instance

Source: Internet
Author: User

Build a Struts2 development environment and complete a HelloWorld instance
Struts2 is a new MVC framework that is merged Based on Struts1 and WebWork technologies. It is very different from Struts1 and cannot be understood as an upgraded version of Struts1. It is based on Xwork, it can be understood as an upgraded version of WebWork.

First, import the jar package is required. For Struts2, at least five core packages need to be imported. The version is selected based on your actual situation:

 

 

You can download the jar package from the official website or add group 102992282 to the group file.

 

Configure the front-end controller filter in web. xml. The Code is as follows:

 

 

     
  
   struts2
      
  
           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter    
      
     
     
  
   struts2
      
  
   /*
      
 

Under src, create a configuration file named struts. xml and add the XML version information and DTD. The Code is as follows:

 

 

 

     

Then write our first Action. The Code is as follows:

 

 

 

Package hxl. insist; public class HelloDemo {public HelloDemo () {System. out. println (instantiate HelloDemo);} public String sayhi () {return hello ;}}

Note: The method is public, the return type is String, and the parameter list is empty.

Configure this Action in the struts. xml configuration file. The Code is as follows:

 

 

 

 
  
  
   
   
   
    
/WEB-INF/page/hello. jsp
   
  
 

 

Note: every time we access this URL, we can see that the console will print the instantiated HelloDemo, so we can know through this phenomenon that every time we access the Action, the web Container creates an Action object, so this Action is thread-safe, but may affect the program efficiency.

 

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.