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
- <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>
<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
- <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
- 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;
- }
- }
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
- <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>
<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
- <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>
<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
- <package name= "user" namespace= "/user" extends= "Struts-default" >
- << Span class= "Tag-name" >action name= "*" class= "com.puckasoft.web.action.UserAction" method=" {1}user "/>
- </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