[Struts2 Study Notes (2)] the default values in the Action configuration and the forwarding types of results in the Action, struts2 Study Notes

Source: Internet
Author: User

[Struts2 Study Notes (2)] the default values in the Action configuration and the forwarding types of results in the Action, struts2 Study Notes
I. default values in Action configuration

<Span style = "font-size: 18px; "> <package name =" itcast "namespace ="/test "extends =" struts-default "> <action name =" helloworld "class =" cn. itcast. action. helloWorldAction "method =" execute "> <result name =" success ">/WEB-INF/page/hello. jsp </result> </action> </package> 1> if no class is specified for action, ActionSupport is used by default. 2> if no method is specified for the action, the execute () method in the action is executed by default. 3> If the name attribute of result is not specified, the default value is success. </Span>

Ii. Various forwarding types of result in Action
<span style="font-size:18px;"><action name="helloworld" class="cn.itcast.action.HelloWorldAction"><result name="success">/WEB-INF/page/hello.jsp</result></action>

(1) result configuration is similar to forward in struts1, but struts2 provides multiple result types. Common types include dispatcher (default), redirect, redirectAction, and plainText.

(2) You can also use the $ {attribute name} expression in the result to access the attributes in the action. The attribute names in the expression correspond to the attributes in the action. As follows:
<result type="redirect">/view.jsp?id=${id}</result>

(3) The following is an example of the redirectAction result type. If the redirected action is in the same package:
<action name="redirectAction"><result type="redirectAction">helloworld</result></action>

If the redirected action is in another namespace:
<Struts> <package name = "itcast" namespace = "/control/employee" extends = "base"> <action name = "list" class = "cn. itcast. action. helloWorldAction "method =" execute "> <result name =" success "type =" redirect ">/employeeAdd. jsp? Username =$ {username} </result> </action> <action name = "redirect"> <result type = "redirect">/employeeAdd. jsp </result> </action> <action name = "redirectAction"> <! -- In different namespaces --> <result type = "redirectAction"> <param name = "actionName"> xxx </param> <! -- Action name --> <param name = "namespace">/control/department </param> </result> </action> </package> <package name = "other "namespace ="/control/department "extends =" base "> <action name =" xxx "> <result>/WEB-INF/page/hello. jsp </result> </action> </package> </struts>


(4) plaintext: display the content of the original file. For example, this type can be used when the source code of the jsp file needs to be displayed as is.
<Result name = "source" type = "plainText"> <param name = "location">/xxx. jsp </param> <param name = "charSet"> UTF-8 </param> <! -- Specify the encoding of the file to be read --> </result>






How to write the result of the struts2 configuration file to jump to the action in other namespaces?

You need to set through param
<Result name = "suc" type = "redirectAction">
<Param name = "namespace">/Tree </param>
<Param name = "actionName"> treeLoadAction </param>
</Result>

Question about struts2 default action configuration in strutsxml

Of course, the path address is like this: localhost: 8080/project name/namespace value/action name value.
Without namespace, the attribute becomes the name value of localhost: 8080/project name // action.

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.