Every operation that an application can perform is referred to as an action. Displaying a Login form, for example, is an action. So is saving a product's details. Creating actions is the most important task in Struts application development. Some actions are as simple as forwarding to a JSP. Others perform logic that needs to be written in action classes.
An action class is an ordinary Java class. It may have properties and methods and must comply with these rules.
• A property must have a get and a set methods. Action property names follow the same rules as JavaBeans property names. A property can be of any type, not only String. Data conversion from String to non-String happens automatically.
• An action class must have a no-argument constructor. If you don't have a constructor in your action class, the Java compiler will create a no-argument constructor for you. However, if you have a constructor that takes one or more arguments, you must write a no-argument constructor. Or else, Struts will not be able to instantiate the class.
• An action class must have at least one method that will be invoked when the action is called.
• An action class may be associated with multiple actions. In this case, the action class may provide a different method for each action. For example, a User action class may have login and logout methods that are mapped to the User_login and User_logout actions, respectively.
• Since Struts 2, unlike Struts 1, creates a new action instance for every HTTP request, an action class does not have to be thread safe.
• Struts 2, unlike Struts 1, by default does not create an HttpSession object. However, a JSP does. Therefore, if you want a completely session free action, add this to the top of all your JSPs:
<%@page session="false"%>
Below is a example of a struts2 Action class:
The Employee action class
package app03a; import java.util.Collection; import java.util.Date; public class Employee { private String firstName; private String lastName; private Date birthDate; private Collection emails; public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public Collection getEmails() { return emails; } public void setEmails(Collection emails) { this.emails = emails; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String register() { // do something here return "success"; } } |
If you implement Action, you will inherit the following static fields: • SUCCESS. Indicates that the action execution was successful and the result view should be shown to the user. • NONE. Indicates that the action execution was successful but no result view should be shown to the user. • ERROR. Indicates that that action execution failed and an error view should be sent to the user. • INPUT. Indicates that input validation failed and the form that had been used to take user input should be shown again. • LOGIN. Indicates that the action could not execute because the user was not logged in and the login view should be shown. You need to know the values of these static fields as you will use the values when configuring results. Here they are. public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; |
0 comments:
Post a Comment