자바 의 Struts 프레임 워 크 에서 append 태그 와 generator 태그 사용

11124 단어 StrutsJava
append 태그:
이 append 탭 들 은 두 개 이상 의 목록 을 매개 변수 로 하고 추가 로 함께 놓 아야 합 니 다.아래 그림 과 같 습 니 다.

<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>
두 개의 목록 이 있 으 면 A 와 B 의 값 은 A1,A2 와 B1,B2 이다.리스트 를 합치 면 A1,A2,B1,B2,append 리스트 는 A1,A2,B1,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 과 depart.ment,저희 도 두 명의 직원 명단 이 있 습 니 다.-employees 와 contractors.getRecruitment Decider 라 는 방법 이 있 습 니 다.Decider 대상 으로 돌아 갑 니 다.Decider 는 true 로 돌아 가 직원 채용 부서 에서 일 하지 않 으 면 false 로 돌아 갑 니 다.
다음은 Department Comparator 비교 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: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.yiibai.struts2.Employee"
     method="execute">
     <result name="success">/employee.jsp</result>
   </action>
  </package>

</struts>

웹.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。다음 화면 을 보 여 드 리 겠 습 니 다.
2015124172633013.jpg (560×267)
generator 태그:
generator 태그 가 교체 기 를 만 드 는 토대 에서 val 속성 을 제공 합 니 다.아래 generator 라벨 은 교체 기 를 만 들 고 교체 기 라벨 로 인쇄 합 니 다.

<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
 <s:iterator>
   <s:property /><br/>
 </s:iterator>
</s:generator>
우리 가 자주 만 나 는 상황 중 일 부 는 목록 이나 배열 에 목록 을 옮 겨 다 녀 야 합 니 다.목록 이나 그룹 을 만 들 거나 scriptlet 를 사용 하거나 generator 탭 을 사용 할 수 있 습 니 다.tag.
액 션 클래스 만 들 기:

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;
  }
}

보기 만 들 기
다음 helloWorld.jsp 는 generator 표 시 를 보 여 줍 니 다.

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>

<h2>Example of Generator Tag</h2>
<h3>The colours of rainbow:</h3>

<s:generator val="%{'Violet,Indigo,Blue,
     Green,Yellow,Orange,Red '}" count="7" 
     separator=",">
  <s:iterator>
   <s:property /><br/>
  </s:iterator>
</s:generator> 
 
</body>
</html>

여기 서 generator 탭 을 만 듭 니 다.쉼표 로 구 분 된 목록 을 포함 하여 무지개 색 을 만 들 었 습 니 다.우 리 는 발전기 라벨 에 구분자 가"라 고 말 했다.우 리 는 모든 7 개의 값 이 목록 에 있 기 를 바란다.만약 우리 가 앞의 세 가지 값 에 만 관심 을 가지 고 있다 면,우 리 는 3 까지 계산 할 것 이다.발전기 가 체 내 에 표시 되 어 있 습 니 다.우 리 는 generator 가 만 든 값 의 인쇄 속성 값 을 표시 하 는 교체 기 를 사용 합 니 다.
프로필
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="hello" 
      class="com.yiibai.struts2.HelloWorldAction" 
      method="execute">
      <result name="success">/HelloWorld.jsp</result>
   </action>

  </package>
</struts>

웹.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/hello.action。다음 화면 을 보 여 드 리 겠 습 니 다.
2015124172721265.jpg (560×297)

좋은 웹페이지 즐겨찾기