Struts 2.0 's Action tutorial

Source: Internet
Author: User
Tags dateformat

Friends with Struts 1.x experience know that action is the core of struts, and of course Struts 2.0 is no exception. However, struts 1.x differs greatly from the action model of Struts 2.0.

Struts 1.x Stuts 2.0
Interface Org.apache.struts.action.Action or its subclasses must be inherited No need to inherit any type or implement any interfaces
form data form data is encapsulated in Formbean The form data is contained in the action, obtained by Getter and setter

Although, theoretically, the action of Struts 2.0 does not need to implement any interfaces or inherit any type, we, for the convenience of implementing the action, In most cases, you will inherit the Com.opensymphony.xwork2.ActionSupport class and Overload (Override) The string execute () method in this class. A specific implementation, as shown in Example 1:

<% @ page contentType = " text/html; charset=UTF-8 " %>
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
  < title > Hello World! </ title >
</ head >
< body >
  < h2 >< s:property value ="message" /></ h2 >
</ body >
</ html >

Example 1 helloworld.jsp

package tutorial;
import java.text.DateFormat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
   private String message;
   public String getMessage() {
     return message;
  }
  @Override
   public String execute() {
    message = " Hello World, Now is " + DateFormat.getInstance().format( new Date());
     return SUCCESS;
  }
}

Example 1 Classes/tutorial/helloworld.java

< package name ="ActionDemo" extends ="struts-default" >
  < action name ="HelloWorld" class ="tutorial.HelloWorld" >
    < result > /HelloWorld.jsp </ result >
  </ action >
</ 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.