Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 append标签
Struts2 append标签
作者:--    发布时间:2019-11-20 10:13:04

说,如果有两个列表a和b的值a1,a2和b1,b2。合并列表,会给你的a1,a2,b1,b2,而append 名单,会有a1,a2,b1,b2。

创建动作类:

首先,让我们创建一个简单的类叫做employee.java,它看起来像:

package com.h3.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;
   }
	
}

employee类有两个属性 - name 和 department,我们也有两个员工名单 - employees 和contractors。我们有一个方法叫做getrecruitmentdecider,返回decider 对象。decider 实现返回true,如果雇员招聘部门工作,否则返回false。

接下来,让我们创建一个departmentcomparator比较employee对象:

package com.h3.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;
	}
}

在上面的例子所示,部门比较的基础上按字母顺序排列的部门员工进行比较。

创建视图

创建一个文件叫做employee.jsp以下内容:

<%@ page contenttype="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>employees</title>
</head>
<body>
   <b>employees and contractors merged together</b>
   <br />
   <s:append  id="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>
</body>
</html>

append标签需要两个或两个以上列出作为参数。我们需要给予追加一个id,这样我们就可以重用它。在这个例子中,我们提供了作为参数传递给员工和承包商的附加标签。然后,我们使用“allemployees”id遍历附加列表和打印员工的细节。

配置文件

struts.xml中应该像这样:

<?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.h3.struts2.employee"
         method="execute">
         <result name="success">/employee.jsp</result>
      </action>
   </package>

</struts>

web.xml中,应该像这样:

<?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>

右键点击项目名称,并单击export > war file 创建一个war文件。然后部署此war在tomcat的webapps目录下。最后,启动tomcat服务器和尝试访问url http://localhost:8080/helloworldstruts2/employee.action。这会给出以下画面:

struts append tag

滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号