This article mainly introduces how to use the append label and generator label in the Struts framework of Java. Struts is one of the three major web development frameworks of Java SSH. For more information, see
Append label:
These append labels need two or more lists as parameters and append them together, as shown in:
If there are two values of List A and list B: A1, A2, B1, and B2. Merging the list will give you A1, A2, B1, B2, while the append list will have A1, A2, B1, B2.
Create a category:
First, let's create a simple class named Employee. java, which looks like:
package com.yiibai.struts2;import java.util.ArrayList;import java.util.List;import org.apache.struts2.util.SubsetIteratorFilter.Decider;public class Employee { 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 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; } }
The Employee class has two attributes: name and department. we also have two Employee lists: employees and contractors. We have a method called getRecruitmentDecider, which returns the Decider object. True is returned for Decider. If an employee works in a recruitment department, false is returned.
Next, let's create a DepartmentComparator to compare the Employee object:
package com.yiibai.struts2;import java.util.Comparator;public class DepartmentComparator implements Comparator { public int compare(Employee e1, Employee e2) { return e1.getDepartment().compareTo(e2.getDepartment()); } @Override public int compare(Object arg0, Object arg1) { return 0; }}
As shown in the preceding example, employees of departments in alphabetical order are compared based on the comparison of departments.
Create View
Create a file named employee. jsp with the following content:
<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>Employees Employees and Contractors Merged together
,
Two or more append labels must be listed as parameters. We need to append an id so that we can reuse it. In this example, we provide additional labels that are passed as parameters to employees and contractors. Then, we use the "allemployees" ID to traverse the additional list and print employee details.
Configuration file
Struts. xml should look like this:
<?xml version="1.0" encoding="UTF-8"?>
/employee.jsp
In web. xml, it should be like this:
<?xml version="1.0" encoding="UTF-8"?>
Struts 2
index.jsp
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
Right-click the project name and choose Export> WAR File to create a WAR File. Then deploy the WAR in the webapps directory of Tomcat. Finally, start the Tomcat server and try to access the URL http: // localhost: 8080/HelloWorldStruts2/employee. action. The following figure is displayed:
Generator label:
The generator tag generates an iterator and provides the val attribute. The following generator tag generates an iterator and prints it out using the iterator tag.
In some situations that we often encounter, we must create a list or traverse the list on an array. You can create a list or array using the scriptlet or generator tag. Tag.
Create action class:
package com.yiibai.struts2;public class HelloWorldAction{ private String name; public String execute() throws Exception { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
Create View
The following helloWorld. jsp displays the generator tag:
<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>Hello WorldExample of Generator TagThe colours of rainbow:
Here, we create a generator tag. we require it to parse strings that contain comma-separated lists to form a rainbow color. We told the generator tag that the separator is "," and we want all seven values to be in the list. If we only care about the first three values, we will set the count to 3. The generator is marked in the body, and we use the iterator to print the attribute value of the value created by the generator tag.
Configuration file
Struts. xml should look like this:
<?xml version="1.0" encoding="UTF-8"?>
/HelloWorld.jsp
Web. xml should be like this:
<?xml version="1.0" encoding="UTF-8"?>
Struts 2
index.jsp
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
Right-click the project name and choose Export> WAR File to create a WAR File. Then deploy the WAR in the webapps directory of Tomcat. Finally, start the Tomcat server and try to access the URL http: // localhost: 8080/HelloWorldStruts2/hello. action. The following figure is displayed: