一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - Java的Struts框架中append標(biāo)簽與generator標(biāo)簽的使用

Java的Struts框架中append標(biāo)簽與generator標(biāo)簽的使用

2020-05-25 15:50java教程網(wǎng) JAVA教程

這篇文章主要介紹了Java的Struts框架中append標(biāo)簽與generator標(biāo)簽的使用方法,Struts是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下

append 標(biāo)簽:
這些append標(biāo)簽需要兩個(gè)或兩個(gè)以上的列表作為參數(shù),并追加它們放在一起,如下圖所示:

?
1
2
3
4
5
6
7
8
<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>

如果有兩個(gè)列表A和B的值A(chǔ)1,A2和B1,B2。合并列表,會(huì)給你的A1,A2,B1,B2,而append 名單,會(huì)有A1,A2,B1,B2。

創(chuàng)建動(dòng)作類:
首先,讓我們創(chuàng)建一個(gè)簡(jiǎn)單的類叫做Employee.java,它看起來(lái)像:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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類有兩個(gè)屬性 - name 和 department,我們也有兩個(gè)員工名單 - employees 和contractors。我們有一個(gè)方法叫做getRecruitmentDecider,返回Decider 對(duì)象。Decider 實(shí)現(xiàn)返回true,如果雇員招聘部門工作,否則返回false。

接下來(lái),讓我們創(chuàng)建一個(gè)DepartmentComparator比較Employee對(duì)象:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
 }
}

在上面的例子所示,部門比較的基礎(chǔ)上按字母順序排列的部門員工進(jìn)行比較。

創(chuàng)建視圖
創(chuàng)建一個(gè)文件叫做employee.jsp以下內(nèi)容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ 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標(biāo)簽需要兩個(gè)或兩個(gè)以上列出作為參數(shù)。我們需要給予追加一個(gè)id,這樣我們就可以重用它。在這個(gè)例子中,我們提供了作為參數(shù)傳遞給員工和承包商的附加標(biāo)簽。然后,我們使用“allemployees”ID遍歷附加列表和打印員工的細(xì)節(jié)。

配置文件
struts.xml中應(yīng)該像這樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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中,應(yīng)該像這樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>

右鍵點(diǎn)擊項(xiàng)目名稱,并單擊Export > WAR File 創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問(wèn)URL http://localhost:8080/HelloWorldStruts2/employee.action。這會(huì)給出以下畫面:

Java的Struts框架中append標(biāo)簽與generator標(biāo)簽的使用

generator 標(biāo)簽:
generator標(biāo)簽生成一個(gè)迭代器的基礎(chǔ)上提供val屬性。以下generator標(biāo)簽生成一個(gè)迭代器,并使用迭代器標(biāo)簽打印出來(lái)。

?
1
2
3
4
5
<s:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
 <s:iterator>
   <s:property /><br/>
 </s:iterator>
</s:generator>

我們經(jīng)常遇到的一些情況,必須創(chuàng)建列表或數(shù)組上遍歷列表。可以創(chuàng)建列表或數(shù)組使用scriptlet或者可以使用generator 標(biāo)簽。 tag.

創(chuàng)建action類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
  }
}

創(chuàng)建視圖
下列 helloWorld.jsp 展示使用generator 標(biāo)記:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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>

在這里,我們創(chuàng)建一個(gè)generator 標(biāo)簽,我們要求它解析的字符串,其中包含逗號(hào)分隔的列表,形成了彩虹的顏色。我們告訴發(fā)電機(jī)標(biāo)簽,分隔符是“,”我們希望所有七個(gè)值在列表中。如果我們只關(guān)心前三個(gè)值,然后我們會(huì)設(shè)置計(jì)數(shù)至3。發(fā)電機(jī)標(biāo)記在體內(nèi),我們使用了迭代器去通過(guò)由generator 標(biāo)記創(chuàng)建的值的打印屬性的值。

配置文件
struts.xml 應(yīng)該像這樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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 應(yīng)該像這樣:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>

右鍵點(diǎn)擊項(xiàng)目名稱,并單擊Export > WAR File 創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問(wèn)URL http://localhost:8080/HelloWorldStruts2/hello.action。這會(huì)給出以下畫面:

Java的Struts框架中append標(biāo)簽與generator標(biāo)簽的使用

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩国产在线一区 | 无人在线视频高清免费观看动漫 | 欧美日韩国产手机在线观看视频 | 亚洲成人免费观看 | 热巴在公交车h文 | 美女的隐私脱裤子无遮挡 | 日韩成人在线免费视频 | 99久久精品无码一区二区毛片 | 国产高清国内精品福利色噜噜 | 日本人妖在线 | 99在线精品免费视频 | 欧美性xxxxxx爱 | 玩乳h文奶水和尚 | 小草视频免费观看在线 | 亚洲精品免费在线观看 | 日本天堂影院在线播放 | 91精品国产亚一区二区三区 | 女教师雪白老汉 | 久久精品无码人妻无码AV蜜臀 | 欧美日韩一区视频 | bbox撕裂bass孕妇 | 国产亚洲精品看片在线观看 | 亚洲天堂2013 | 红色一片在线影视 | 精品国产一区二区三区在线 | 金发美女与黑人做爰 | 男女刺激高清视频在线观看 | 国产亚洲综合精品一区二区三区 | 无人区大片免费播放器 | 国产亚洲女在线精品 | 99久久精品99999久久 | 欧美一区二区不卡视频 | 国内精品久久久久久中文字幕 | 久久久久久久99精品免费观看 | 久久re热在线视频精6 | 暴露狂婷婷 | sex5·性屋娱乐| 精品九九视频 | 亚洲国产欧美在线看片 | 色哟哟在线资源 | 国产精品久久久久a影院 |