Action configuration and use of STRUTS2

Source: Internet
Author: User

One, struts configuration file

Struts2 By default loads the Struts.xml, Struts-default.xml, Struts-plugin.xml three types of files, where Struts.xml is a developer-defined default profile, Struts-default.xml is the STRUTS2 framework's own configuration file, and Struts-plugin.xml is the STRUTS2 plug-in's default profile.

The most important role of the Struts.xml profile is to configure the response between the action and the request and to configure the correspondence between the logical attempt name and the physical view resource. In addition, Struts.xml files have additional features such as bean configuration, constant configuration, importing other profiles, and so on.

Struts2 constants are equivalent to properties that work for the STRUTS2 application as a whole, so Struts2 constants are often referred to as Struts2 attributes.

Although there are three ways to STRUTS2 configuration constants: Through Struts.properties files, through struts.xml configuration files, web.xml files that are applied through the web. It is often recommended that you define the STRUTS2 attribute in the Struts.xml file, rather than the Struts2 attribute in the Struts.properties file.

You can decompose a Struts.xml profile into multiple configuration files, and then include additional configuration files in the Struts.xml file, as shown in the following example:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE Struts Public

"-//apache softwarefoundation//dtd Struts Configuration 2.1.7//en"

"Http://struts.apache.org/dtds/struts-2.1.7.dtd" >

<!--Specifies the root element of the Struts 2 configuration file-->

<struts>

<!--import additional configuration files via the Include element-->

<includefile= "Struts-part1.xml"/>

<includefile= "Struts-part2.xml"/>

...

</struts>

The included Struts-part1.xml file is a standard Struts2 configuration file that contains information such as DTD information, root elements of Struts2 configuration files, and so on. Typically, all of the configuration files for Struts2 are placed under the web-inf/classes path of the Web application. The Struts.xml file contains additional configuration files, and Struts.xml files are loaded by the STRUTS2 framework, which allows all configuration information to be loaded in. second, to achieve action 1.Action Introduction

The action is the core of the application, and the developer needs to provide a large number of action classes and configure the action in the Struts.xml file. The action class contains the processing logic for the user request, and the action class is also called the business controller.

The action class is a pojo that contains the setter and getter methods that handle the user request by default: the Execute () method.

Actionsupport is a default action implementation class. If our configured action does not specify the class attribute (that is, no user provides an action Class), the system automatically uses the Actionsupport class as the action handler class. 2.Action access servlet API

Because the action class is not servletapi coupled, it is easier to test the action. The servlet APIs that are typically accessed in Web applications are HttpServletRequest, HttpSession, and ServletContext, which represent the request in the JSP built-in object, the three interfaces, Session and application. Method One: Struts2 provides a actioncontext class that the Struts2 action can use to access the servlet API. The common methods used in classes are as follows:

Object get (Object key): The GetAttribute (Stringname) method equivalent to HttpServletRequest.

Map getapplication (): Returns a Map object that simulates the ServletContext instance of the application.

Static Actioncontext GetContext (): Statically method to get Actioncontext instance of the system.

Map getparameters (): Gets all request parameters. Similar to the HttpServletRequest Getparametermap ().

Map getsession (): Returns a Map object that simulates a httpsession instance.

void Setapplication (Map application): The key-value that is passed into a map, converted to application property name, property value.

void Setsession (Map session): A key-value that is passed into a map, converted to the property name of the session, the property value. Method Two: Struts2 provides the following several interfaces (need to implements and implement methods):

Servletcontextaware: The action that implements this interface can directly access the ServletContext instance of the Web application.

Servletrequestaware: The action that implements this interface can directly access the HttpServletRequest instance of the user request.

Servletresponseaware: The action that implements this interface can directly access the HttpServletResponse instance of the server response. method Three: Use the Servletactioncontext (tool Class) to access the servlet API:

Static PageContext Getpagecontext (): Gets the PageContext object for the Web application.

Static httpservletrequest getrequest (): Gets the HttpServletRequest object for the Web application.

Static httpservletresponse GetResponse (): Gets the HttpServletResponse object for the Web application.

Static ServletContext getservletcontext (): Gets the ServletContext object for the Web application.

3. Configure Action

Once you have implemented the action processing class, you can configure the action in the Struts.xml file. Configuring the action is to let STRUTS2 know which action handles which request, that is, to complete the correspondence between the user request and the action. We can assume that action is the basic "program unit" of STRUTS2; 3.1 packages and namespaces

        struts2 uses packages to organize the action, so the action definition is done under the package definition, and the action is defined by using the < Package.../> under the

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.