The use of Append tags and generator tags in the Java Struts Framework _mysql

Source: Internet
Author: User
Tags generator tomcat tomcat server

Append Tags:
These append tags require two or more lists as arguments and append them together, as shown in the following illustration:

<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 you have a value of two lists A and B a1,a2 and B1,B2. Merging lists will give you the A1,A2,B1,B2, and the Append list will have a1,a2,b1,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 append label requires two or more two 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. We then use the "Allemployees" ID to traverse the attached list 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:

Generator Tags:
the generator tag generates an iterator based on the Val attribute provided. The following generator tag generates an iterator and prints it using the iterator label.

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

We often encounter situations where we have to create a list or an array of traversal lists. You can create lists or arrays using Scriptlet or you can use generator labels. Tag.

To create an 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 a View
The following helloworld.jsp display uses the generator tag:

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

Here we create a generator tag that we ask it to parse the string, which contains a comma-delimited list that forms the color of the rainbow. We tell the generator tag that the separator is "," We want all seven values in the list. If we only care about the first three values, then we'll set the count to 3. The generator is marked in the body, we use the iterator to pass the value of the print property of the value created by the generator tag.

Configuration file
Struts.xml should be 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=" /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/hello.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.