The use of append and generator labels in the Struts framework of Java,

Source: Internet
Author: User
Tags java spring framework

The use of append and generator labels in the Struts framework of Java,

Append label:
These append labels need two or more lists as parameters and append them together, as shown in:

<s:append var="myAppendIterator">   <s:param value="%{myList1}" />   <s:param value="%{myList2}" />   <s:param value="%{myList3}" /></s:append><s:iterator value="%{#myAppendIterator}">   <s:property /></s:iterator>

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"%>

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"?><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>  <constant name="struts.devMode" value="true" />  <package name="helloworld" extends="struts-default">   <action name="employee"      class="com.yiibai.struts2.Employee"     method="execute">     <result name="success">/employee.jsp</result>   </action>  </package></struts>

In web. xml, it should be like this:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  id="WebApp_ID" version="3.0">    <display-name>Struts 2</display-name>  <welcome-file-list>   <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>   <filter-name>struts2</filter-name>   <filter-class>     org.apache.struts2.dispatcher.FilterDispatcher   </filter-class>  </filter>  <filter-mapping>   <filter-name>struts2</filter-name>   <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

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.

<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}"> <s:iterator>   <s:property /><br/> </s:iterator></s:generator>

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" %>

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"?><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.devMode" value="true" />  <package name="helloworld" extends="struts-default">      <action name="hello"       class="com.yiibai.struts2.HelloWorldAction"       method="execute">      <result name="success">/HelloWorld.jsp</result>   </action>  </package></struts>

Web. xml should be like this:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  id="WebApp_ID" version="3.0">    <display-name>Struts 2</display-name>  <welcome-file-list>   <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>   <filter-name>struts2</filter-name>   <filter-class>     org.apache.struts2.dispatcher.FilterDispatcher   </filter-class>  </filter>  <filter-mapping>   <filter-name>struts2</filter-name>   <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

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:

Articles you may be interested in:
  • Briefly describe how to use merge labels in Java Struts Framework
  • Detailed instructions on the use of if/else labels in the Struts framework of Java
  • Introduction to the architecture and installation configuration of Java Spring framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.