In order to reduce the number of actions since learning the STRUTS2 framework, we can use dynamic methods for processing.
dynamic Method Invocation,dmi refers to the action of a FORM element that is not directly equal to the name of an action, but rather by
Use an exclamation mark (!) in the name of the action. ) to identify the name of the method to invoke, in the format actionname! Methodname.action.
However, because the invocation of a dynamic method may pose a security risk (any method in the action can be performed via a URL), when determining the use of dynamic method calls, the
You should make sure that all the methods in the action are normal, open methods. Based on this property, STRUTS2 provides a property for the configuration to suppress the invocation of a dynamic method.
We can set the property struts.enable.DynamicMethodInvocation to false through the constant element in the Struts.xml file. Ban
Invoke dynamic methods such as:
<name= "Struts.enable.DynamicMethodInvocation" value= "false" >
The following summarizes the differences between the method property invocation and the dynamic method invocation:
A dynamic method call is used if the request to be processed by a different method of the same action uses the same configuration (result, etc.).
If the invocation of a different method requires a different configuration, use the method property of the action element to configure multiple names for the same action
However, using the method property causes a large number of action configurations to be present in the configuration file.
From a security standpoint, it is recommended that the method attribute be used to implement different methods of the same action to handle different requests.
Using the method attribute is more secure based on security considerations, but the new problem comes with the gradual increase in action, resulting in a large number of action configurations in the Struts.xml file.
So we can take a wildcard in action.
when configuring the <action.../> element, you need to specify the Name,class and method properties, where the Name property supports wildcards, and then you can use the expression in the Class,method property. this way of using wildcards is another form of dynamic method invocation. The wildcard Shing number (*) indicates that 0 or more strings are configured. Let's implement the wildcard character with a complete example.
First we create a good Web project and create a good entity class
Next we define the method in the entity class
Package cn.lxp.action; Import Com.opensymphony.xwork2.ActionSupport; Public class extends Actionsupport {
The methods in the two entity classes are the same
PublicString Add ()throwsException {
return"Add"; } PublicString Update ()throwsException { return"Update"; } PublicString list ()throwsException { return"List"; } PublicString Delete ()throwsException { return"Delete"; } }
Next, we'll configure it in the Struts.xml file.
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/ Dtds/struts-2.1.dtd "><Struts> <!--developerment Mode: The following configuration is modified after the development model, no need to restart the Tmocat server - <constantname= "Struts.devmode"value= "true" /> <!--Create a default package that inherits the Struts-deafult package from struts 2 of struts 2 - < Packagename= "Default"namespace="/"extends= "Struts-default"> <Actionname="*_*"class= "Cn.lxp.action." {1} "Method= "{2}"> <resultname= "{2}">/{1}/{2}.jsp</result> <resultname= "Success">/index.jsp</result> </Action> </ Package></Struts>
Then we do the relevant JSP page test can be
Finally, start the Tomcat server and access it in the browser address bar.
So the dynamic method is called successfully, will you?
Struts 2 Dynamic method call, no, come on.