Struts2控制標籤
阿新 • • 發佈:2018-12-09
1、ifelse標籤
一共有這三類標籤,用法大致相同。 1. < s:if > 2. < s:ifelse > 3. < s:else >
example:
hello.jsp
<form action="hello">
<label >Please pick a name</label><br/>
<select name="name">
<option name="Mike">Mike</option>
<option name="Jason">Jason</option>
<option name="Mark">Mark</option>
</select>
<input type="submit" value="Say Hello"/>
</form>
helloworld.jsp
<body>
<b>Example of If and Else</b><br/>
<s:if test="name == Mike">
You have select Mike
</s:if>
<s:elseif test="name == Jason">
You have select Jason
</s:elseif>
<s:else>
You don't select Mike and Jason
</s:else>
</body>
hello.jsp選擇姓名後交提表單,跳轉到helloworld.jsp。 在if與else標籤中進行判斷,輸出相應資訊。
2、iterator標籤
example:
<body >
<b>Example of Iterator Tag</b><br/>
<s:iterator value="employees">
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
<b>Employees sorted by Department</b><br/>
<s:bean name="cn.w3cschool.struts2.DepartmentComparator"
var="deptComparator" />
<s:sort comparator="deptComparator" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:sort>
<br/><br/>
<b>SubSet Tag - Employees working in Recruitment department </b><br/>
<s:subset decider="recruitmentDecider" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:subset>
<br/><br/>
<b>SubSet Tag - Employees working in Accounts department </b><br/>
<s:subset decider="accountsDecider" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:subset>
<br/><br/>
<b>SubSet Tag - Employees 2 and 3 </b><br/>
<s:subset start="1" count="2" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:subset>
</body>
public class Employee extends ActionSupport {
private String name;
private String department;
public Employee(){}
public Employee(String name, String department) {
this.name = name;
this.department = department;
}
private List employees;
private List contractors;
public String execute() {
employees = new ArrayList();
employees.add(new Employee("George","Recruitment"));
employees.add(new Employee("Danielle","Accounts"));
employees.add(new Employee("Melissa","Recruitment"));
employees.add(new Employee("Rose","Accounts"));
contractors = new ArrayList();
contractors.add(new Employee("Mindy","Database"));
contractors.add(new Employee("Vanessa","Network"));
return "success";
}
public Decider getRecruitmentDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
Employee employee = (Employee) element;
return employee.getDepartment().equals("Recruitment");
}
};
}
public Decider getAccountsDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
Employee employee = (Employee) element;
return employee.getDepartment().equals("Accounts");
}
};
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List getEmployees() {
return employees;
}
public void setEmployees(List employees) {
this.employees = employees;
}
public List getContractors() {
return contractors;
}
public void setContractors(List contractors) {
this.contractors = contractors;
}
}
建立一個Employee類,同時建立成員變數 List型別的employees與String型別的name和department。
decider="accountsDecider"
該屬性比較特別,需要在Employee類中建立方法getAccountsDecider(),注意名字對應不要錯誤。
public Decider getAccountsDecider() {
return new Decider() {
public boolean decide(Object element) throws Exception {
Employee employee = (Employee) element;
return employee.getDepartment().equals("Accounts");
}
};
}
這段程式碼需要建立一個DepartmentComparator類,在該類中重寫compare方法,進行需要的判斷。
<s:bean name="cn.w3cschool.struts2.DepartmentComparator"
var="deptComparator" />
<s:sort comparator="deptComparator" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:sort>
public class DepartmentComparator implements Comparator {
public int compare(Employee e1, Employee e2) {
return e1.getDepartment().compareTo(e2.getDepartment());
}
@Override
public int compare(Object o1, Object o2) {
return 0;
}
}
取子集,從記錄從0計數,所以取第二條與第三條記錄。
<s:subset start="1" count="2" source="employees">
<s:iterator>
<s:property value="name"/> ,
<s:property value="department"/><br/>
</s:iterator>
</s:subset>
3、merge標籤
example: 一樣是使用上面的Employee類。
<s:merge var="allemployees">
<s:param value="employees"/>
<s:param value="contractors"/>
</s:merge>
<s:iterator value="allemployees">
<s:property value="name"/>
<s:property value="department"/><br/>
</s:iterator>
merge標籤作用為把兩個表合成一個表,但是與接下來的append標籤有一些不同。 為merge標籤新增var屬性,即可在其他標籤中呼叫。在merge標籤內,param標籤指定表,employees與contractors為兩個不同表。
4、append標籤
example: 一樣是使用上面的Employee類。
<s:append var="allemployees">
<s:param value="employees"/>
<s:param value="contractors"/>
</s:append>
<s:iterator value="allemployees">
<s:property value="name"/>
<s:property value="department"/><br/>
</s:iterator>
5、generator標籤
example:
<s:generator separator="," val="%{'red, blue, black, pink, white, green'}">
<s:iterator>
<s:property/><br/>
</s:iterator>
</s:generator>
分隔符為‘,’。 網頁輸出為: