Structs2 structs. XML configuration

Keywords: Java Apache xml Struts JSP

<struts>
    <package name="action" namespace="/action" extends="struts-default">
        <action name="LoginAction" class="action.LoginAction" method="execute">
            <result name="teacher">/teacher.jsp</result>
            <result name="student">/student.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

    <include file=""></include>
</struts>

namespace is commonly used /, or / package name.

When the xml file is large, you can configure it with multiple xml files, such as using one xml under each package to configure the action of the package, and using < include > in structs.xml to include other xml configuration files.

 

 

 

 

Setting up a method for processing requests

<package name="action" namespace="/action" extends="struts-default">
<global-allowed-methods></global-allowed-methods>
<action name="loginAction" class="action.LoginAction" method="exe">
<allowed-methods></allowed-methods>
</action>
</package>

The method attribute can be set in <action>, in <allowed-methods> </allowed-methods>, or in <global-allowed-methods> </global-allowed-methods>.

Since it is methods, plural form, you can set multiple method names.

 

Configuration in structs-default.xml:

<global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods>

 

 

 

Set up page invocation mode

<action name="loginAction" class="action.LoginAction">
      <result name="" type="dispatcher"></result>
</action>

type attributes can be set individually in <result> or globally in structs-default.xml:

<result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.result.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.result.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.result.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.result.PlainTextResult" />
            <result-type name="postback" class="org.apache.struts2.result.PostbackResult" />
        </result-types>

Set the default attribute to true. The default is dispatcher, forwarding.

 

 

 

 

Setting up interceptors

<package name="action" namespace="/action" extends="struts-default">
        <interceptors>
            <interceptor name="" class=""></interceptor>
            <interceptor-stack name="">
                <interceptor-ref name="" />
                <interceptor-ref name="" />
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name=""></default-interceptor-ref>

        <action name="loginAction" class="action.LoginAction" method="exe">
            <interceptor-ref name=""></interceptor-ref>
            <interceptor-ref name=""></interceptor-ref>
            
        </action>
    </package>

 

Register the interceptor and interceptor stack in <interceptors>.

Register an interceptor, name specifies the interceptor name, and class specifies the java class corresponding to the interceptor.

<interceptor-stack> Register an interceptor stack, and name specifies the name of the interceptor stack. <interceptor-ref/> Specifies the interceptor to be referenced.

 

You can use <interceptor-ref name="> </interceptor-ref> in <action> to set up the interceptor and interceptor stack currently used in <action>, one <interceptor-ref name="> </interceptor-ref> refers to an interceptor and interceptor stack, and you can use multiple <interceptor-ref name="> </interceptor-ref> at the same time.

The default interceptor and interceptor stack can also be set in <default-interceptor-ref name="> </default-interceptor-ref>, which is valid for all action s under this package.

The reference interceptor and interceptor stack can be defined by themselves, or they can be built into structs-default.xml.

 

 

 

The sub-elements of <package> have a certain order, and <action> should be put at the end.

Configuration can be modeled on structs-default.xml.

Posted by edwinlcy on Thu, 05 Sep 2019 01:21:41 -0700