I learned from the last chapter OGNL stay struts2 The embodiment of the application of , They are the acquisition of parameters 、 Transfer of data in configuration file , The other one is struts2 label , But it wasn't recorded in the previous chapter , This chapter is a learning record ~



Struts2 label ——OGNL The expression in Struts2 Application in



Struts2 label ——OGNL The expression in Struts2 Application in

Struts2 There are mainly 、、 And the four , Here is a detailed study of the four ~

1、 Control tags

The main function of this control tag is to control the output of data in a set , For example, output from the background of a List The value of the set 、 The judgment of the attributes of the set, etc ;
First , Create a data set List, The code is as follows :

Code :

package com.java.action.tag;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class tag1 extends ActionSupport{
	
	public String execute() throws Exception {
		List<String> list = new ArrayList<>();
		list.add("list1");
		list.add("list2");
		list.add("list3");
		list.add("list4");
		ActionContext.getContext().put("list", list);
		return "success";
	}
}

such , And we'll get one called list Set , from jsp Show how to operate in the page ~

(1)if、elseif、else: Judgment operation

<s:if test="#list.size()==2">
 The length of this array is 2!
</s:if>
<s:elseif test="#list.size()==3">
 The length of this array is 3!
</s:elseif>
<s:else>
 The length of this array is not 2 Not for 3!
</s:else>

Running results :
 Picture description of running results

(2) Traverse the output List The value of the set
There are two ways to traverse the values of the output set , But there's not much difference between the two , You can see ~

  • The first one is :
<s:iterator value="#list">
	<s:property />
</s:iterator>

  • The second kind :
<s:iterator value="#list" var="index">
	<s:property value="index"/><br>
</s:iterator>

Running results :
 Picture description of running results
The difference between the two traversals is that the second convenient way to define the collection is to set “ Alias ”~
This tag is a special tag for traversal , Used to output data from a set one by one ;

expand :
Struts2 My label is mainly struts2 Within the framework of jsp The page gets the label of the parameter , Need to know struts2 The main expressions used by the framework are OGNL( Object view navigation language ), What is used is also OGNL The expression of , About struts2 There are two authentic ways for frame pages to get values , There's only a slight difference between the two ~
Assuming that the action The name of the parameter set passed in the class is list,User The properties of an object are (cust_name、cust_age、cust_phone、cust_gender、cust_dir、cust_mobile) What is stored in it is obtained from the database customer Object , Put it in ActionContext.getContext in ( It means to use OGNL The value of the expression should be preceded by “#”);
Get the value to traverse the output :

The first one is :

	<s:iterator value="#list">
		<s:property value="cust_name" />
		<s:property value="cust_age" />
		<s:property value="cust_phone" />
		<s:property value="cust_gender" />
		<s:property value="cust_dir" />
		<s:property value="cust_mobile" />
	</s:iterator>

The second kind :

    <s:iterator value="#list" var="cust">
		<s:property value="#cust.cust_name" />
		<s:property value="#cust.cust_age" />
		<s:property value="#cust.cust_phone" />
		<s:property value="#cust.cust_gender" />
		<s:property value="#cust.cust_dir" />
		<s:property value="#cust.cust_mobile" />
    </s:iterator>

The two ways to get values are basically the same , It's all through OGNL Expression to receive parameters , This paper explains the label writing of these two ways to get values :
 Explain the picture description


2、 Data labels

The main thing is to use this property ;

<s:iterator begin="0" step="1" end="100">
	<s:property/>--
</s:iterator>

Interpretation of the :
begin=“0”: The starting number is zero , from 0 Start stacking the output ;
step=“1”: Each time interval is 1( It can be understood as adding one more number at a time 1);
end=“100”: Run to the number added to 100 stop it ;

Running results :
 Picture description of running results


3、 Form labels

stay struts2 There's a set of labels for your own form in , Its usage is as follows :

Code :

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ValueStack</title>
</head>
<body>
	<s:form action="tag1" namespace="/">
		<s:textfield label=" user name " name="name" />
		<s:password label=" password " name="password" />
		<s:radio list="#{1:' male ',0:' Woman '}" name="gender" label=" Gender " "></s:radio>
		<s:select list="#{0:' Primary school ',1:' Junior high school ',2:' high school ',3:' Specialty ',4:' Undergraduate ',5:' master ' }" name="edu" label=" Education "  headerKey="" headerValue="--- Please select ---" ></s:select>
		<s:checkboxlist list="#{0:' swimming ',1:' calligraphy ',2:' Mountaineering ',3:' The stroke ',4:' go fishing ',5:' Basketball ',6:' football ',7:' Table tennis ',8:'LOL',9:'DOTA',10:'CF' }" name="hobby" label=" hobby " ></s:checkboxlist>
		<s:file name="photo" label=" Photo "></s:file>
		<s:textarea name="intor" label=" Self introduction. "></s:textarea>
		<s:submit value=" Submit " />
	</s:form>
</body>
</html>

Code :

@SuppressWarnings("serial")
public class tag1 extends ActionSupport{
	private String name;
	public String execute() throws Exception {
		System.out.println(name);
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Code :

<action name="tag1" class="com.java.action.tag.tag1" method="execute">
	<result name="success" type="dispatcher">/tag1.jsp</result>
</action>

Running results :
 Picture description of running results
Use struts2 Of form Label benefits :
1、 Struts2 Of form Tags have their own built-in styles ( Can be jsp After the page runs, you will find that table All kinds of labels );
2、 The submitted data will be echoed to the input box , Based on the data submitted in the stack , The code in the input box is omitted ;


4、 Non form labels

stay tage2.action Return in method success Before adding , And then in tag2.jsp Put one in to receive action Error message in , The specific operation is shown in the following figure :

Code :

package com.java.action.tag;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class tag2 extends ActionSupport{
	private String name;
	public String execute() throws Exception {
		this.addActionError(" Error message !");
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Code :

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ValueStack</title>
</head>
<body>
	<s:actionerror/>
</body>
</html>

Code :

<action name="tag2" class="com.java.action.tag.tag2" method="execute">
	<result name="success" type="dispatcher">/tag2.jsp</result>
</action>

Results after operation :
 function tag2.action After the result picture description


pass: This is it. OGNL The expression in struts2 One of the usages in reflects , In fact, the most important thing is to traverse the output ~


《END》