Struts2 Framework Study Notes 2: detailed configuration, struts2 Study Notes

Source: Internet
Author: User
Tags string find

Struts2 Framework Study Notes 2: detailed configuration, struts2 Study Notes

Core configuration file:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Package: encapsulate the Action configuration. you can configure many actions in the Package. name attribute: name of the package. start at will. the other package names cannot be repeated. namespace attribute: defines a namespace extends attribute (required) in the action access path: inherits a specified package (default is used here) abstract attribute (Supplement ): indicates whether the package is abstract or not. the package cannot run independently. specially inherited --> <package name = "hello" namespace = "/hello" extends = "struts-default"> <! -- Action element: configure the action class name attribute: determines the name of the resource accessed by the Action. Note that the attribute of the class is not the identity function. The complete class name method attribute of the action class: specifies the method in the Action class to process the request --> <action name = "HelloAction" class = "hello. helloAction "method =" hello "> <! -- Result element: name attribute of result configuration: name of result processing. corresponds to the return value of the action method. type attribute: Specifies the result class to be called to process the result. The forward dispatcher is used by default. TAG body: Enter the relative path of the page --> <result name = "success" type = "dispatcher">/hello. jsp </result> </action> </package> </struts>

 

Introduce other configuration files:

    <include file="dynamic/struts.xml"></include>

Relative Path under src

 

Constant Configuration:

There is a default properties configuration file under the struts2 core package. When we need to modify it,

 

Example 1:

Create a configuration file by yourself.

Struts. properties:

struts.i18n.encoding=UTF-8

 

Example 2:

Write in the core configuration file:

    <constant name="struts.i18n.encoding" value="UTF-8"></constant>

 

Example 3:

In web. xml:

  <context-param>      <param-name>struts.i18n.encoding</param-name>      <param-value>UTF-8</param-value>  </context-param>

 

In actual development, only the second type is used, The other two can be understood.

 

Other constant configurations (in the core configuration file ):

<! -- I18n: International. solve post submission garbled (do not solve get garbled, solve it by yourself) --> <constant name = "struts. i18n. encoding "value =" UTF-8 "> </constant> <! -- Specifies the extension name used to access the action, which must be used to access the Action. do http: // localhost: 8080/struts2/hello/HelloAction. do --> <constant name = "struts. action. extension "value =" do "> </constant> <! -- Specify whether struts2 runs in development mode. hot load master configuration. (No need to restart to take effect) 2. more error messages are provided to facilitate debugging during development --> <constant name = "struts. devMode "value =" true "> </constant>

Several constant configurations are used in Spring. We will not introduce them here.

 

 

Dynamic method call:

Here, an Action contains multiple methods:

Package dynamic; // call the public class Demo1Action {public String add () {System. out. println ("add user! "); Return" success ";} public String delete () {System. out. println (" delete a user! "); Return" success ";} public String update () {System. out. println (" Modify user! "); Return" success ";} public String find () {System. out. println (" find a user! "); Return" success ";}}

Create an XML file under the current package and introduce it in the main configuration file

(The first method does not comply with the development specifications)

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Dynamic method call method 1 (not recommended) Configure whether to enable a constant for dynamic method call. By default, the constant is disabled (false). You need to enable the configuration and then access it ...... /dynamic/Demo1Action! Add (! Fixed format) --> <constant name = "struts. enable. dynamicMethodInvocation "value =" false "> </constant> <package name =" dynamic "namespace ="/dynamic "extends =" struts-default "> <! -- Dynamic method call Method 2: wildcard (recommended) Use {1} to retrieve the content with the first asterisk wildcard for access:/dynamic/Demo1Action_add --> <! -- Method 1 <action name = "Demo1Action" class = "dynamic. demo1Action "> --> <action name =" Demo1Action _ * "class =" dynamic. demo1Action "method =" {1} "> <result name =" success ">/hello. jsp </result> </action> </package> </struts>

 

Struts2 Default Configuration:

Package default; // test the default configuration public class Demo2Action {public String execute () {System. out. println ("default"); return "success ";}}

 

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name = "default" namespace = "/default" extends = "struts-default"> <! -- If the action under the package is not found, Demo2Action is used as the default action to process the request --> <default-action-ref name = "Demo2Action"> </default-action-ref> <! -- Default attribute of method: execute --> <! -- Default property of result name: success --> <! -- Default attribute of result type: dispatcher forwarding --> <! -- Default class property: com. opensymphony. xwork2.ActionSupport --> <action name = "Demo2Action"> <result>/hello. jsp </result> </action> </package> </struts>

 

Method is not configured here, but the execute method is used by default.

Default Value of class: locate the class and call its execute method. This method returns "success" directly"

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.