For those of you who have used Struts2, we need to configure multiple Action attribute tags in the Struts2 configuration file struts2. XML when we have too many Action methods. However, when the number of Actions is more than 10, this method becomes less convenient. Here, we use three methods to dynamically invoke Actions through the Struts2 framework
[1] Configure the action property [2] Invoke it with an exclamation point [3] Invoke it with a wildcard
Next, we will explain the use of the above three methods:
【1】 By configuring the action property
First of all, there are two methods in our action --add and update. The core code is as follows:
public String add()
{ return SUCCESS; } public String update() { return SUCCESS; } Next we need to configure it in the struts.xml file, where the name attribute of the action does not have the same name, followed by the method attribute of the action, indicating which method of the action is called
<struts> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" > </result> </result> </action> <action name="addAction"method="add" > </result> </action> <action name="updateAction"method="update" BBB </result BBB </action> </package> </ Struts >
When we type in the address barhttp://localhost:8080/struts2…, you can call the HelloWorld action’s add method, and the result is as shown in the figure below:
[2] Call by way of exclamation mark
To use this method, you should start with a sentence in the struts.xml configuration file:
<constant name="struts.enable.DynamicMethodInvocation" value="true"</constant>
The purpose of using this sentence is to allow Struts to dynamically invoke methods. The struts.xml configuration file after the action is configured as follows:
<package name=”default” namespace=”/” extends=”struts-default”>
> <result>/result.jsp</result> <result name="add">/add.jsp</result> <result name="update">/update.jsp</result> </action> </package>
In addition, it is necessary to modify the return value in the action related method, and the execution time is success. However, when this method dynamically calls the action, the value after return should be consistent with the name value of the result attribute in the action tag, as shown in the figure below:
public String add()
{
return "add";
}
public String update()
{
return "update";
}
The address entered in the address bar should also be changed accordingly, as follows:
http://localhost:8080/struts2…, the results are as follows:
[3] By wildcard (officially recommended)
The value attribute in the constant tag in the struts.xml configuration file should be changed to false. In addition, other parts of the struts.xml file should be modified as follows:
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld_*" method="{1}"
class="cn.imooc.action.HelloWorldAction">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
The action and methods in the second method In the address bar enter the address of the different, specific as follows: http://localhost:8080/struts2… And the result looks like the picture above so these are the three ways to call the action, so hopefully you found that helpful.