Action Method invocation

Source: Internet
Author: User

The access path to the action access Path action is the namespace of the package that is configured in the Struts.xml file, and the action's name and common struts.action.extension together determine for example:

XML code
  1. <constant name="struts.action.extension" value="action," />
  2. <package name="Default" namespace="/" extends="Struts-default">
  3. <action name="Hello">
  4. <result>/1.jsp</result>
  5. </Action>
  6. </Package>
  7. <package name="a" namespace= "/A" extends="Struts-default">
  8. <action name="Hello">
  9. <result>/2.jsp</result>
  10. </Action>
  11. </Package>
    <constant name= "Struts.action.extension" value= "action,"/>    <package name= "default"  namespace= "/ "  extends=" Struts-default ">       <action name=" Hello ">         <result>/1.jsp</result>       </action>    </package>        <package name= "a"  namespace= "/a"  extends= " Struts-default ">       <action name=" Hello ">         <result>/2.jsp</result>       </action >    </package>

To access the action named Hello under the default package, run the path:/hello or/hello.action access the action with the name "Hello" under Package A, run path:/a/hello or/a/hello.action If you modify the value of struts.action.extension to do

XML code
    1. <constant name="struts.action.extension" value="do,action" />
<constant name= "struts.action.extension" value= "Do,action"/>

Access the action with the name "Hello" under Package A, the path must be run:/a/hello.do or/a/hello.action

The namespace of the package is different for each package, the action in the package cannot have the same name, the action of different packages can have the same name as the same web app with the same name, and you can avoid the access violation by placing them under different namespaces. If an action is not found in the namespace, it is found in the default namespace, and is not found in the default namespace.

According to the above configuration access/hello.action then jump/1.jsp access/a/hello.action jump/2.jsp access/b/hello.action then jump/1.jsp access/b/hi.action will not find the page, report different Often

The action method call and the dynamic method call to the action receive request call the Execute method by default, but this does not satisfy our needs. Usually we define all the requests for a business module in the same action class, which requires the action class to provide more methods to handle different requests than just an Execute method. For example, we want to define a user module to delete and change the request, you can take the following ways to achieve

Java code
  1. Package com.puckasoft.web.action;
  2. Public class Useraction {
  3. Public String Saveuser () throws exception{
  4. System.out.println ("Add User");
  5. return null;
  6. }
  7. Public String UpdateUser () throws exception{
  8. System.out.println ("Modify user");
  9. return null;
  10. }
  11. Public String DeleteUser () throws exception{
  12. System.out.println ("Delete user");
  13. return null;
  14. }
  15. Public String Showuser () throws exception{
  16. SYSTEM.OUT.PRINTLN ("Query user");
  17. return null;
  18. }
  19. }
Package Com.puckasoft.web.action;public class Useraction {public String saveuser () throws exception{  System.out.println ("Add User");  return null; } public String UpdateUser () throws exception{  System.out.println ("Modify user");  return null; } public String DeleteUser () throws exception{  System.out.println ("delete user");  return null; } public String Showuser () throws exception{  System.out.println ("Query user");  return null; }}

Configuration Method One: Action method invocation

XML code
  1. <package name="user" namespace="/user" extends="Struts-default">
  2. <action name="Save" class="com.puckasoft.web.action.UserAction"
  3. method="Saveuser"></action>
  4. <action name="Update" class="com.puckasoft.web.action.UserAction"
  5. method="UpdateUser"></action>
  6. <action name="Delete" class="com.puckasoft.web.action.UserAction"
  7. method="DeleteUser"></action>
  8. <action name="show" class="com.puckasoft.web.action.UserAction"
  9. method="Showuser"></action>
  10. </Package>
    <package name= "user"  namespace= "/user"  extends= "Struts-default" >      <action name= "Save"  class= "Com.puckasoft.web.action.UserAction"        method= "Saveuser" ></action>      <action name= " Update "  class=" com.puckasoft.web.action.UserAction "        method=" UpdateUser "></action>      < Action name= "Delete"  class= "com.puckasoft.web.action.UserAction"        method= "DeleteUser" ></action >      <action name= "show"  class= "com.puckasoft.web.action.UserAction"        method= "Showuser" > </action>    </package>

Run path Path format: The name of the package namespace/action

User add/user/save.action User update/user/update.action user Delete/user/delete.action user View/user/show.acton configuration method Two: action Dynamic method Invoke DMI

XML code
  1. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  2. <package name="user" namespace="/user" extends="Struts-default">
  3. <action name="index" class="com.puckasoft.web.action.UserAction"/>
  4. </Package>
<constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/><package name= "user"  namespace = "/user"  extends= "Struts-default" >     <action name= "index"  class= " Com.puckasoft.web.action.UserAction "/></package>

Run path Format: Package namespace/action's name! The method name defined in the Action class users add/user/index!saveuser.action user update/user/index!updateuser.action user Delete/user/index!deleteuser.ac tion user view/user/index!showuser.acton only set constant struts.enable.DynamicMethodInvocation to true to perform a dynamic call, which is turned on by default.

Configuration method Three: Use wildcards to define the action use wildcards to minimize the amount of configuration. You can use the * wildcard character in the Name property of the action element, which can match multiple consecutive characters except/or, in the class and method properties of the action element, and in the result element, you can refer to what the wildcard character actually matched during the visit

XML code
    1. <package name= "user"   namespace= "/user"   extends= "Struts-default" >  
    2.   << Span class= "Tag-name" >action name= "*" class= "com.puckasoft.web.action.UserAction" method=" {1}user "/> 
    3. </package> 
<package name= "user"  namespace= "/user"  extends= "Struts-default" >  <action name= "*" class= " Com.puckasoft.web.action.UserAction "method=" {1}user "/></package>

Run path Path format: The name of the package namespace/action

User add/user/save.action User update/user/update.action user Delete/user/delete.action user view/user/show.acton

Action Method invocation

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.