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 >