课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Struts2 merge标签

当前位置:免费教程 » Java相关 » Struts2

假设你有A和B两个列表,值为A1,A2和B1,B2。合并列表将得出A1,B1,A2,B2。

创建Action类

首先让我们创建一个简单的类Employee.java,如下:

  1. package com.w3xue.struts2;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.struts2.util.SubsetIteratorFilter.Decider;
  5. public class Employee {
  6. private String name;
  7. private String department;
  8. public Employee(){}
  9. public Employee(String name,String department)
  10. {
  11. this.name = name;
  12. this.department = department;
  13. }
  14. private List employees;
  15. private List contractors;
  16. public String execute() {
  17. employees = new ArrayList();
  18. employees.add(new Employee("George","Recruitment"));
  19. employees.add(new Employee("Danielle","Accounts"));
  20. employees.add(new Employee("Melissa","Recruitment"));
  21. employees.add(new Employee("Rose","Accounts"));
  22. contractors = new ArrayList();
  23. contractors.add(new Employee("Mindy","Database"));
  24. contractors.add(new Employee("Vanessa","Network"));
  25. return "success";
  26. }
  27. public Decider getRecruitmentDecider() {
  28. return new Decider() {
  29. public boolean decide(Object element) throws Exception {
  30. Employee employee = (Employee)element;
  31. return employee.getDepartment().equals("Recruitment");
  32. }
  33. };
  34. }
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. public String getDepartment() {
  42. return department;
  43. }
  44. public void setDepartment(String department) {
  45. this.department = department;
  46. }
  47. public List getEmployees() {
  48. return employees;
  49. }
  50. public void setEmployees(List employees) {
  51. this.employees = employees;
  52. }
  53. public List getContractors() {
  54. return contractors;
  55. }
  56. public void setContractors(List contractors) {
  57. this.contractors = contractors;
  58. }
  59. }
Employee类有两个属性:namedepartment,我们还有两个employee列表:永久的employeescontractors。我们有一个名为getRecruitmentDecider的方法,它返回一个Decider对象。如果员工在recruitment(招聘)部门工作,则Decider实现返回true,否则返回false
接下来,创建一个DepartmentComparator来比较Employee对象:

  1. package com.w3xue.struts2;
  2. import java.util.Comparator;
  3. public class DepartmentComparator implements Comparator {
  4. public int compare(Employee e1, Employee e2) {
  5. return e1.getDepartment().compareTo(e2.getDepartment());
  6. }
  7. @Override
  8. public int compare(Object arg0, Object arg1) {
  9. return 0;
  10. }
  11. }
如上例所示,部门比较器按字母顺序比较部门的员工。

创建视图

创建一个名为employee.jsp的文件,包含以下内容:

  1. <%@ page contentType="text/html; charset=UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4. <head>
  5. <title>Employees</title>
  6. </head>
  7. <body>
  8. <b>Employees and Contractors Merged together</b>
  9. <br />
  10. <s:merge id="allemployees">
  11. <s:param value="employees" />
  12. <s:param value="contractors" />
  13. </s:merge>
  14. <s:iterator value="allemployees">
  15. <s:property value="name"/>,
  16. <s:property value="department"/><br/>
  17. </s:iterator>
  18. </body>
  19. </html>
merge标签需要两个或更多列表作为参数。我们需要给merge标签一个id,以便以后可以重新使用它。在此示例中,我们提供employees和contractors作为merge标签的参数。然后,使用“allemployees”id迭代合并列表并打印员工详细信息。

配置文件

你的struts.xml应该如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true" />
  7. <package name="helloworld" extends="struts-default">
  8. <action name="employee"
  9. class="com.tutorialspoint.struts2.Employee"
  10. method="execute">
  11. <result name="success">/employee.jsp</result>
  12. </action>
  13. </package>
  14. </struts>
你的web.xml应该如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <display-name>Struts 2</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <filter>
  13. <filter-name>struts2</filter-name>
  14. <filter-class>
  15. org.apache.struts2.dispatcher.FilterDispatcher
  16. </filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>struts2</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>
右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/employee.jsp,将显示以下界面:

系统信息
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号