[SSH for beginners] Detailed description of struts2 value stack and the struts2 tag library + ognl expression, struts2ognl
I. What is a value Stack: A storage mechanism provided by struts2, similar to domain objects and value stacks, which can store values and values
Features: Advanced and later, the top element is called the stack top, also called the pressure stack.
<S: debug> </s: debug> the stack elements are displayed. The stack structure is root and context. The structure returned by root is the list set, and the context returns the map set.
Ii. Differences between action and servlet:
Each time you access an action, a new action object is created, and the servlet creates only one object.
Note: each time you access the action, an action object will be created. There is only one value stack object in the action object.
3. Value stack objects:
ActionContext ac = ActionContext. getContext ();
ValueStack v1 = ac. getValueStack ();
It is not commonly used. Generally, ognl expressions are used for values. Below are several examples, before that, import the tag library <% @ taglib prefix = "s" uri = "/struts-tags" %> in jsp.
3. Save the variable and set the value to the value stack.
01): Save the variable
Private String name;
Public String getName (){
Return name;
}
@ Override
Public String execute () throws Exception {
Name = "James ";
}
02): Value
<S: property value = "name"/>
4. Save the object to the value stack and set the value:
01): Create a preson class, instantiate it, and obtain the get method.
Private Person person Person = new Person ();
Public Person getPerson (){
Return person;
}
@ Override
Public String execute () throws Exception {
Person. setName ("Zhang ");
Person. setAge (1 );
Person. setSex ("female ");
}
02): Value (Object Name. attribute name ):
<S: property value = "person. name"/>
<S: property value = "person. age"/>
<S: property value = "person. sex"/>
5. Save the set to the value stack and set the value
01): instantiate a list set.
Private List <Person> lp = new ArrayList <Person> ();
Public List <Person> getLp (){
Return lp;
}
@ Override
Public String execute () throws Exception {
Lp. add (new Person ("Li Si", 1, "male "));
Lp. add (new Person ("Wang Wu", 2, "female "));
}
02): Value
<! -- Take 01 --> <s: property value = "lp [0]. name "/> <s: property value =" lp [1]. name "/> <br> <! -- Take the value of the Set 02 --> <s: iterator value = "lp"> <s: property value = "name"/> <s: property value = "age"/> <s: property value = "sex"/> <br> </s: iterator> <! -- Take the value of the Set 03. After adding the var attribute, it takes the value from the context. Therefore, add the # number --> <s: iterator value = "lp" var = "l"> <s: property value = "# l. name "/> <s: property value =" # l. age "/> <s: property value =" # l. sex "/> </s: iterator>
6. Add the value of the domain object to the value stack.
01): Add to the value Stack
@ Override
Public String execute () throws Exception {
// Domain object
HttpServletRequest request = ServletActionContext. getRequest ();
Request. setAttribute ("abc", "domain object Value ");
Return SUCCESS;
}
02): Value
<S: property value = "# request. abc"/>
7. Use the attribute value in the form Tag:
<S: textfield name = "text" value = "% {name}"> </s: textfield> (you can convert it using %)
8. form label:
<! -- From tag -->
<S: form action = "" method = "post">
<! -- Equivalent to text box -->
<S: textfield> </s: textfield>
<! -- Password box -->
<S: password> </s: password>
<! -- A single queue must have a name attribute value -->
<S: radio list = "# {'1': 'male', '2': 'female '}" value = "1" name = ""> </s: radio>
<! -- Select multiple boxes. The name attribute must be -->
<S: checkboxlist list = "# {'1': 'pipisha', '2': 'sweet and sour fish ', '3 ': 'fried rice cake '} "value =" 1 "name =" "> </s: checkboxlist>
<! -- In the drop-down box, combox will bring a text box to bind the value -->
<S: select list = "# {'1': 'prawns ', '2': 'sweet and sour fish', '3': 'fried rice cake '}"> </s: select>
<! -- Submit -->
<S: submit> </s: submit>
<! -- Reset -->
<S: reset> </s: reset>
</S: form>
: