SSH Tour (ii)--STRUTS2 configuration file

Source: Internet
Author: User

First contact with the Java SSH Framework, using the MyEclipse development tool, and the Tomcat hot deployment, do not understand how to access the JSP page, how to access the action invoke Business Logic layer service. Start with the struts configuration file now!

From the opening page, if you access the Product Details page for a id=18?

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >http://localhost:8080/MyShop/product_findByPid.action?pid=81</span>
Myshop is the name of my project, before ". Action" is the corresponding action action name. Struts2 intercepts this part of the string, looking for the Findbypid method under product in the configuration file. The following struts configuration files:
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><span style= "White-space:pre" ></span></span><pre name= "code" class= "HTML" ><span Style= "White-space:pre" ></span><!--product Module Action---<action name= "product_*" class= "productaction "Method=" {1} ">
<span><span style= "Color:rgb (63, 95, 191); ><!--</span><span style= "Color:rgb (63, 95, 191); > defines the mapping relationship between the processing result string and the resource </span><span style= "Color:rgb (63, 95, 191); >--></span></span><result name= "findbypid" >/web-inf/jsp/product.jsp</result></ Action>

without the system to learn the SSH principle, can not clear these configuration, call relationship, will not tune the bug, out of the wrong and do not know where the problem. This is explained in the hierarchical form of the tree.

1. Package Packages

The core components in the STRUTS2 Framework are action, interceptors, etc., and the STRUTS2 framework uses packages to manage actions and interceptors. Each package is more than one action, multiple interceptors, and so on. Its properties are:

Name: Required property to specify the name of the package. Extends: Optional attribute that specifies that the package inherits other packages.    Inheriting other packages, you can inherit the action definitions, interceptor definitions, and so on in other packages. namespace: Optional attribute that specifies the namespace for the package.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >      <package name= "Myshop" extends= "Struts-default" namespace= "/" ></package></span>

The browser address is accessed according to the namespace/action name, and this namespace is a virtual path that may not exist in the project. However, in order to conveniently find JSP pages in the project, the name of the namespace is matched in the class/jsp path.


2. Namespace Configuration

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >      <!--product Module action---</span><span style= "Font-family:simsun; font-size:14px;" >      </span><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >      <action name= "product_*" class= "myShop.product.action.ProductAction" method= "{1}" >      </action ></span>
This class is a namespace path, with different namespaces, which can allow the same name, which is a problem for new items. You can actually manage the action object through spring:

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >      <!--action--></span><pre name= "code" class= "HTML" ><span style= "in the Spring product module font-family:simsun;font-size:14px; " >       </span><span style= "Font-family:simsun;" ><bean id= "productaction" class= "MyShop.product.action.ProductAction" scope= "prototype" ></span>
<property name= "Productservice" ref= "Productservice"/> </bean>

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><!--struts under action---<action name= "product_*" class= "productaction" method= "{1}" ></action ></span>

When Struts2 is used alone, the action is created by STRUTS2 itself; When integrated with spring, the Action object instance is created by spring, and the class property of struts is the id attribute value of the bean that is configured in spring's applicationcontext.xml.


3. Configure the Method property for action:

Method= "{1}" means the first of the wildcard characters to be called, and the wildcard character and the string below are found together.

4. Configuration processing Result:

Struts2 configures the results by using the <result .../> element in the Struts.xml file. The STRUTS2 provides two result configurations.

Local result: The <result .../> is configured as a child element of the <action ...> element.
Global Result: <result .../> is configured as a child element of the <global-results ...> element.


In summary, the Struts.xml file is primarily responsible for managing the action mappings in your application, and the mapping between action processing results and physical resources. And for the action in the STRUTS2, is the controller, the action as a hub, inherit the Actionsupport class, implement modeldrive model driver, inject various attributes, generate Get/set method, Call service.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SSH Tour (ii)--STRUTS2 configuration file

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.