I learned from the last chapter struts2 Constant configuration of , What we learn in this chapter is struts2 The method dynamic call of is related to the previous chapter , amount to struts2 Advanced version of constant configuration ~ The detailed study is as follows



struts2 Dynamic method call


1、struts2 Dynamic method call

First , Prepare one action class , Class name B_dynamic, There are multiple businesses in this class , For example, add, delete, change and query users , A simple call returns success And print the corresponding text to the console ; The design is as follows :

B_dynamic:

package com.java.B_dynamic;

public class Demo1 {
	public String add() {
		System.out.println(" add to ---");
		return "success";
	}
	public String delete() {
		System.out.println(" Delete ---");
		return "success";
	}
	public String update() {
		System.out.println(" to update ---");
		return "success";
	}
	public String select() {
		System.out.println(" lookup ---");
		return "success";
	}
}


1.1、 Dynamic method call mode

There are two ways to dynamically call , Namely :
【1】 Dynamic method calls related to constant configuration
【2】 Constant configuration independent wildcard thought dynamic method call
Here are two ways to learn in detail ~

1.2、 Dynamic method calls related to constant configuration

First , Configure dynamic method calls in the core configuration file , Default false Value ( The closed position ) Set to true( open ); Its name Values and value All values can be in the struts. Properties Found in file , The common constants listed above are also introduced ;
struts. Properties File dynamic method call settings
Turn on the dynamic call configuration code as follows ( In the core configuration file ), Note that in the following code action It doesn't exist in the tag method attribute ;
 Turn on dynamic call mode

Input the corresponding path directly after configuration , After typing action Add an exclamation mark after the name of “!”, In English , Then add the method name of the corresponding method after the exclamation mark , Click enter to see the output text of the corresponding method in the console ;
 Test dynamic calls


1.3、 Constant configuration independent wildcard thought dynamic method call

This way of dynamically calling methods has nothing to do with the configuration of constants , Using the idea of wildcard space occupying , The code is easy , As long as in the core configuration file action In the tag name The property value of the property is set to “xxx_*” And will be method The property value of the property is written as “{1}” that will do ;
The settings are as follows :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<package name="dynamic" namespace="/dynamic" extends="struts-default">
<action name="Demo1_*" class="com.java.B_dynamic.Demo1" method="{1}">
<result name="success">/dynamic.jsp</result>
</action>
</package>
</struts>

 Constant configuration independent wildcard idea, dynamic method call mode setting style
The access method is , Write the method name directly after the underline ;
 Dynamic call test path style
When in jsp Page to submit the form, jump to a action Class time should be written as ( Take the example above ):href="${pageContext.request.contextPath}/Demo1_xxx", This xxx It's dead , Represents which method name this operation should connect to ~
Only in this way is the real dynamic method call , It is to configure the operation method before jump ;

1.4、 Summary of dynamic method calls

The most common one is the second one , The way of dynamic calling with the idea of wildcard ~


pass: The next chapter is about struts2 in action Method creation ~

《 End of this chapter 》