"Web Development Learning note" STRUCTS2 Action learning Note (i)

Source: Internet
Author: User

1. Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter Preparation and execution
2.
  <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</ Url-pattern>  </filter-mapping>
url-pattern contract cooked to write * *, no need to write *.action
3.
<package name= "Default" namespace= "/" extends= "Struts-default" >     <action name= "Hello" >         < result>         /hello.jsp         </result>     </action>    <span style= "White-space:pre" ></ Span></package>
   Namespace= "/" and the path meaning corresponding to the access;
    /hello.action
    The action was accessed, giving us the result of result;
    The default action can be omitted;
4. Associated structs source code and Java docs
Jar file Right-click->properies->java Source Attachment
    Set source code
        D:/program Files/struts-2.1.6/src/core/src/main/java
    Javadoc documentation
        File:/d:/program files/struts-2.1.6/docs/struts2-core/apidocs/
    To set the XML hint:
        A) window–preferences– search Catalog–add

B) Select key type as URI

c) key:http://struts.apache.org/dtds/struts-2.0.dtd

D) Location: the corresponding DTD file, located in the Struts-core package, unzip open, specify the corresponding position, such as D:/program files/struts-2.1.6 \lib\struts2-core-2.1.6\s Truts-2.0.dtd
5. Structs Operation mechanism
Find the filter Dofilter method , WebApplication, Web. XML, URL------HTTP request--Tomacat, client-side->
  <filter>  <filter-name>struts2</filter-name>  <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</ Url-pattern>  </filter-mapping>
, reference Struct.xml, find the corresponding action, find corresponding result, feedback result, request Forwad to the destination file
  <package name= "Default" namespace= "/" extends= "Struts-default" ><action name= "index" ><result>/ Index.jsp</result></action> </package>
6, namespace determines the access path of the action, the default is "", you can receive all the path action
Namespace can be written as/, or/xxx, or/xxx/yyy,
The corresponding action access path is/index.action,/xxx/index.action, or/xxx/yyy/index.action.
Namespace it is best to also use a module to name
   <constant name= "Struts.devmode" value= "true"/>    <package name= "front" extends= "Struts-default" Namespace= "/front" >        <action name= "index" >            <result>/Namespace.jsp</result>        < /action>    </package>         <package name= "main" extends= "Struts-default" namespace= "" >        < Action name= "index" >            <result>/Namespace.jsp</result>        </action>    </package >
Struct.xml Analysis
Package role: To avoid the name of action and conflict issues;
namespace= "/front", must start with a slash, namespace preferably also use a module to name;
not writing namespace, equivalent to Namespace = "", means that as long as the action is found, it is given to
namespace treatment;
Process: First find the action under the corresponding path to match, if not, find namespace is empty action,
If the action is not found, an error will be added;
7, the return of the specific view can be determined by the user's own definition of action;
The specific method is to find the corresponding configuration item according to the returned string, to determine the contents of the view;
<constant name= "Struts.devmode" value= "true"/><package name= "front" extends= "Struts-default" namespace= "/" ><action name= "index" class= "Com.struts2.front.action.Index" ><result name= "Success" >/ Actionintroduction.jsp</result></action> </package>
the implementation of the specific action can be a common Java class, with the public String Execute method can be implemented or
Action interface;
    Package com.bjsxt.struts2.front.action;    Import com.opensymphony.xwork2.Action;    public class IndexAction1 implements Action {@Overridepublic String execute () {return "Success";}    }
    Configuration Analysis:
<class= "Com.bjsxt.struts2.front.action.Index" >
struct.xml, find the corresponding class-I instantiation object, execute the corresponding execute () method
Execution Process:
Read XML---action is class-a class object found (each access must be a new object)
When class is not configured, the default class is Actionsupport.

Actionsupport Source

   Public String Execute () throws Exception {        return SUCCESS;}

The most common is inherited from Actionsupport, the advantage is that you can directly use the Struts2 package good method

    Package com.struts2.front.action;    Import Com.opensymphony.xwork2.ActionSupport;    public class IndexAction2 extends Actionsupport {@Overridepublic String execute () {return ' success ';}    }

Cause: Actionsupport has encapsulated a number of methods that can be called directly within a subclass, which can be used directly in subclasses.

The above content is based on the horse Soldier Teacher's tutorial organized.

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.