简单说明Java的Struts框架中merge标签的使用方法
merge标签合并标记需要两个或两个以上的列表作为参数,并把它们合并在一起,如下所示:
<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>
如果有两个列表a和b的值,a1,a2和b1,b2。合并列表,会给出a1,b1,a2,b2。
创建动作类:
首先,让我们创建一个简单的类叫做employee.java,它看起来像:
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; } }
employee类有两个属性 - name 和 department,我们也有两个员工名单 - employees 和contractors。我们有一个方法叫做getrecruitmentdecider,返回decider 对象。decider 实现返回true,如果雇员招聘部门工作,否则返回false。
接下来,让我们创建一个departmentcomparator 比较employee对象:
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; } }
在上面的例子所示,部门比较的基础上按字母顺序排列的部门员工进行比较。
创建视图
创建一个文件叫做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:merge id="allemployees"> <s:param value="employees" /> <s:param value="contractors" /> </s:merge> <s:iterator value="allemployees"> <s:property value="name"/>, <s:property value="department"/><br/> </s:iterator> </body> </html>
合并标记需要两个或两个以上的列表作为参数。我们需要给合并一个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.yiibai.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。这会给出以下画面:
上一篇: php使用parse_str实现查询字符串解析到变量中的方法
下一篇: 基于axios的二次封装