Simple description of how to use the merge tag in the Java Struts Framework _java

Source: Internet
Author: User
Tags tomcat server

Merge label merging tags require two or more than two lists as parameters and merge them together as follows:

<s:merge var= "Mymergediterator" >
   <s:param value= "%{mylist1}"/> <s:param value=
   "%{myList2}" />
   <s:param value= "%{mylist3}"/>
</s:merge>
<s:iterator value= "%{#myMergedIterator } ">
   <s:property/>
</s:iterator>

If there is a value of two lists A and B, A1,a2 and B1,B2. The merge list gives the A1,B1,A2,B2.

To create an action class:
First, let's create a simple class called Employee.java, which looks like this:

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"; 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;
  The public void Setemployees (List employees) {this.employees = employees;
  Public List getcontractors () {return contractors;
  The public void setcontractors (List contractors) {this.contractors = contractors;

 }
 
}

The employee class has two attributes-name and department, and we also have two employee lists-employees and contractors. We have a method called Getrecruitmentdecider, which returns the decider object. The decider implementation returns TRUE if the employee recruiting department works, it returns false.

Next, let's create a Departmentcomparator comparison 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 example above, departmental employees are compared in alphabetical order based on department comparisons.

Create a View
Creating a file is called employee.jsp the following:

<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%@ taglib prefix=" s "uri="/struts-tags "%>
 
 

The merge tag requires two or more lists as arguments. We need to merge an ID so that we can reuse it. In this example, we provide a merged label that is passed as a parameter to the employee and the contractor. We then use the "allemployees" id merge list to traverse and print the details of the employee.

Configuration file
The 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=" /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-f ile-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <fi lter-name>struts2</filter-name> <filter-class> Org.apache.struts2.dispatcher.FilterDispatcher &L t;/filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <u rl-pattern>/*</url-pattern> </filter-mapping> </web-app> 

Right-click the project name and click Export > War file to create a war document. This war is then deployed under Tomcat's WebApps directory. Finally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/employee.action. This will give you the following picture:

Related Article

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.