Several ways to implement action in struts 2
Action is used to process user requests, so it is also called business controller. Each action class is a work unit. The struts 2 framework is responsible for matching the user's request with the corresponding action. If the matching is successful, the action class will be called to process the user's request, and the matching rules need to be declared in the struts 2 configuration file.
There are three ways to implement action class under struts 2 Framework:
POJO implementation mode
Take user login as an example, create loginaction class.
Login page:
Error page:
Success page:
struts. xml:
In this way, an action is implemented in the form of POJO. When you click the login button, the data in the form will be submitted to login Action, the struts 2 framework will automatically call the setter method of loginaction, encapsulate the request parameter value into the corresponding attribute, and execute () method.
Implementation of action interface
In order to make the action class more standardized and make the string style returned by the execute() method written by various developers consistent, struts 2 provides an action interface, which defines the general specification that the acicoin processing class should implement:
The following code uses the action interface to create an action class:
struts. xml:
Method of inheriting actionsupport class
Struts 2 framework provides an implementation class actionsupport for action interface, which provides many default methods. Inheriting actionsupport class when writing action class will greatly simplify the development of action. Actionsupport class is the default action processing class of struts 2. If the class attribute is not specified when configuring the action class, the system automatically uses actionsupport class as the action processing class by default.
The following code creates an action class by inheriting the actionsupport class and overrides the validate() validation method:
The above code adds a validate() method to validate the data submitted by the form. This method will run before executing the execute() method. If it is found that the data submitted by the form does not meet the requirements, execute the addfielderror() method, write the error information into the field error list fielderrors of the action class, and automatically return to the input view, Let the user re-enter the form and submit it.
In struts Add input view in XML configuration file:
When the data submitted by the form fails to pass the verification, it will return to the input page, and the program will still "stay" (it looks like this, but it is actually a new input page) on the input page login.jsp.
Access actioncontext
Compared with struts 1, an important improvement of struts 2 is that the action is no longer coupled with any servlet API, but sometimes the action class cannot implement business logic without accessing the servlet API (for example, tracking the status of HTTP session). At this time, the action needs to access the httpsession in the servlet API.
Struts 2 provides an easier way to access the servlet API. In the struts 2 framework, action can access the servlet API through the actioncontext class. Actioncontext provides methods to read and write HttpServletRequest, httpsession and data in the servlet API.
Common methods are as follows:
The following code demonstrates the use of action to access actioncontext
The above code first uses actioncontext The getContext () static method gets the ActionContext object of the system, then calls the getApplication () method of the ActionContext object to get the Map object corresponding to the ServletContext, and then calls the get () /put () method to do the data read / write operation. Finally, save the num value to the ServletContext.
Write clicknum JSP page to access the application:
The above code creates a form with the action attribute value of clicknum and action. When you click Submit, the form will be submitted to Counteraction for processing.
In struts Add counteraction configuration in the XML configuration file:
Access servlet API
Although struts 2 provides actioncontext to directly access the servlet API. But sometimes you need to access the sevlet API directly. In order to directly access the servlet API in action, struts 2 also provides a series of other interfaces. By implementing these interfaces, action can directly access the servlet API.
The interfaces for accessing servlet API provided by struts 2 framework are shown in the following table:
The following code takes the implementation of the sevletrequestaware interface as an example, and saves the login successful user name to the session by obtaining httpsession:
Loginaction4 defined in the above code implements the sevletrequestaware interface, and rewrites the setservletrequest() method in the interface. The parameter of setservletrequest() method is the HttpServletRequest object. When running the web application, the struts 2 framework will automatically pass the current request object into the setservletrequest() method, and then assign the request object to the request attribute of loginaction4, In this way, the request object can be accessed in other methods of loginaction4 class. Through the request object, you can obtain the httpsession object and save the current user information to the session.
Add login The action attribute of the form in the JSP page is changed to login4 action:
Create first JSP displays user information:
Create another second jsp:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.