The last chapter records OGNL And so on , This chapter is about OGNL Expression and Struts2 The combination of learning and learning , Detailed notes are as follows ~

Struts2 And OGNL The combination of expressions

1、Struts2 And OGNL The combination of expressions

1.1、Struts2 And OGNL The combination principle of expressions

OGNL To run an expression, you have to have OGNLContext, stay struts2 There is OGNLContext But its name is ValueStack( valuestack ), It's also made up of two parts , Namely Root and Context,Root It's a stack , The stack holds the currently accessed Action,Context Where is the ActionContext( Data Center );
ValueStack( valuestack ) Picture description

1.2、Root The principle of stack

The principle is simple , As long as you know “ First in, then out , Last in, first out , No matter how the stack value changes, the index is continuous ” Just OK 了 , It's called stack pressing , Out of the stack is called Bomb stack , The picture is as follows :
Root Picture description of the principle of stack
stay struts2 The medium voltage stack uses “push()”, The practical way to play stack is “pop()”, These methods are in ValueStack Class ( establish action Inherited when ActionSupport,ActionSupport Inherited from ValueStack), Can be called directly ;

1.3、Struts2 And OGNL The embodiment of combination

Struts2 And OGNL The combination is reflected in the following aspects :

  1. Parameter acquisition
  2. The configuration file
  3. Struts2 label ( There is no detailed record in this chapter , The next chapter is about learning )

Let's learn one by one ~

1.3.1 、 Parameter acquisition
The parameters are obtained in struts2 There are four in all , They are attribute driven 、 Object driven, model driven, and collection encapsulation , I've learned how to use it before , Now we mainly study the principle and process of parameter acquisition , There's no need to talk about it one by one , There are many model drivers used in normal development , So take model driven learning as an example ;

Code :

public class Demo_stack2 extends ActionSupport implements ModelDriven<User>{
	private User u = new User();
	public String execute() throws Exception {
		return SUCCESS;
	public User getModel() {
		return u;

There is a knowledge point hidden in this code , That's to get the stack ,ActionContext It's a central database , There's all kinds of data in it , stay struts2 Middle stack (ValueStack) Contains ActionContext, But the same ActionContext It also includes ValueStack Data information , So you can call ActionStack Of getContext() Methodical getValueStack() Method to get the stack ;

expand :
When using model driven, it's impossible to follow the general process , Make sure that the received parameters are pushed into the stack before assignment , Once the assignment is successful, the parameters can no longer be stored in the station , If you want to push the parameter into the stack before the assignment, you must call the assignment action Push the parameters into the stack before ,struts2 It's from filter Filter call action, That is to say, it's in filter The filter calls the corresponding action We have to finish stack pressing before , And in the struts2 in filter The order of execution is relatively high , This needs to find one with higher execution order to stack the parameters , Generally, the method is chosen to carry out these businesses , So we can write in our handwriting action Implementation interface in , And in handwritten action Rewrite the method in , And write the parameter stack pressing method into the rewriting method , In this way, we can ensure that the parameters are loaded on the stack first and then assigned ;

however !!! stay struts2 Because this method is more troublesome , therefore struts2 The team encapsulated this block operation into another interface , This interface is , The steps of stack pressing are directly omitted ( In the encapsulated interface, the method has been automatically called in the background for stack pressing ), So we only have a few lines of code above ;

Code :

<form action="${pageContext.request.contextPath }/Demo_stack2" method="post">
	name:<input type="text" name="name" ><br>
	age:<input type="number" name="age"><br>
	<input type="submit" value="OK"> 

Code :

<!-- OGNL And struts2 combination  ===  Model driven transfer parameters    -->
   <action name="Demo_stack2" class="" method="execute">
      <result name="success" type="dispatcher">/valueStack.jsp</result>

test :
Jsp The interface parameters input the data as shown below :
 Input data picture description

Console output :
 Console output picture description

1.3.2 、 Profile aspect
Use OGNL Expression for data transfer , The location of the transmission is very special , Is in action And configuration files , The code is shown below :

Code :

//  Dynamically passing parameters to the configuration file  --- struts2 And OGNL Combined with the embodiment of the configuration file 
public class Demo_stack3 extends ActionSupport{
	private String game_name;
	public String execute() throws Exception {
		game_name = "SUCCESS_1";//  Pretend it's data from a database 
		return SUCCESS;
	public String getGame_name() {
		return game_name;

	public void setGame_name(String game_name) {
		this.game_name = game_name;

Code :

<!--  adopt OGNL Dynamically add data to the path as a pass parameter  -->
		<action name="Demo_stack3" class=""
			<result name="success" type="redirectAction">
				<param name="actionName">Demo_stack2</param>
				<param name="namespace">/</param>
				<!-- Information configured in the configuration file struts Can't recognize , By default, the parameter is attached to the path passed   If you want to set parameters to be dynamic, you can use "${}" The parcel OGNL Expression for dynamic value ; -->
				<param name="name">${game_name}</param>

This is only done when redirecting to action In the jump type of ;
It involves “Demo_stack2” This action Configuration file for , It has detailed code configuration , I won't go into that ~

test :
Input Demo_stack3.class The path is in the address bar :
 Input Demo_stack3.class The path is described in the picture in the address bar
Click enter to see the address bar :
 See the picture description in the address bar

success ~

pass:struts2 Tags are learned in the next chapter ~