STRUTS2 Action Configuration

Source: Internet
Author: User
Tags abstract
Configuration of Action

Regardless of the implementation of the action, to run correctly, you need to configure it in Struts.xml, which is the basis for using action. Configuration of <package>

The action needs to be configured in Struts.xml to be used, and the action should be configured as a child of the <package> element, so what is the function of the <package> element.

The <package> element encapsulates a logically related set of actions, Result, Intercepter, and other elements to form a separate module, which can inherit from other packages or be inherited by another package as a parent. , such as the configuration "<package name=" HelloWorld "extends=" Struts-default ">", HelloWorld This package inherits Struts-default this package.

The <package> element has the following properties:

Name: The names of the packages. Must be configured

Extends: The package to inherit, which is later configured with the name of the inherited package. Options available

Namespace: The namespace of the package. Options available

Abstract: Defines a package as abstract, that is, cannot contain the definition of an action. Options available

Namespace configuration is the namespace of the package, the same namespace cannot have the same name action, of course, different namespaces can have the same name action. Similar to the functionality of Java-like packages, namespace can effectively prevent conflicting action names, because after configuring namespace, you need to add namespace as the action prefix when you access the action. If you do not configure namespace, which means the default namespace, you do not need to add a namespace prefix when accessing it.

Abstract is used to define the package as abstract, that is, it cannot contain the definition of action, but the abstraction package can be inherited by other packages, so it can define the elements that other packages need, such as Resulttype, Interceptor, and so on. Configuration of <action>

Although the action is very important in the development of STRUTS2, its configuration is very simple and the basic rules are as follows:

The <action> element is a sub-element of the <package> element and should be configured within the <package> element

<action> elements typically need to configure the name and class attributes, where name is required

The <action> element can contain additional child elements such as <param>, <result>, <interceptor-ref>, <exception-mapping >

This is usually the configuration of the name and class attributes, and then the configuration <result> child elements.

Why you can not configure the <action> class property.

The extends property of the

 <package> element configures "Struts-default", which shows that the package inherits a package called "Struts-default", which is Struts2 the default configuration of each < The public configuration of the package> element. Open it and you can see that this file is similar to our familiar struts.xml. The root element is also the <struts>,<struts> element below which has a child element <package>, and the value of this <package> Name property is "Struts-default". Look at the final configuration <default-class-ref class= "Com.opensymphony.xwork2.ActionSupport"/>, which shows that when the <action> element does not write the class attribute, The default configuration is to use the Actionsupport class. What is the use of this approach? In most cases it is necessary to configure the value of the class property of <action>, as we need to write our own action class configuration to handle the request instead of using the default configuration. Consider a situation, in the actual development of the security requirements of the Web project, often put JSP in the Web-inf folder, so as to prevent the outside world directly through the URL to access the JSP page, then the JSP must be servlet or action after the subsequent page, can be accessed. Therefore, if we have a JSP page under Web-inf, but before it does not need an action to access the logic layer, it is equivalent to directly accessing this JSP page. This JSP can be used as a successor to the Actionsupport page, using Default-class-ref to reduce the duplication of configuration.

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.