Struts2 bean 標籤

bean標籤的設置和push 標籤的組合,它可以創建一個新的對象實例,然後設置變數的值。然後,它的bean可以在值棧,因此,它可用於在JSP頁面。

bean標籤需要一個Java Bean來工作。所以,標準的java bean規律應遵循。這是bean 應該有一個無參數的構造函數。要公開和使用的所有屬性的getter和setter 方法​​。對於這個練習的目的,讓我們用下麵的計數器類,在struts util包。Counter 類是一個bean,可以使用一個計數器來跟蹤。 

因此,讓我們所有的檔保持不變,並修改的helloWorld.jsp檔。

創建動作類:

package com.zaixian.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:

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

<s:bean name="org.apache.struts2.util.Counter" var="counter">
   <s:param name="first" value="20"/>
   <s:param name="last" value="25" />
</s:bean>
<ul>
   <s:iterator value="#counter">
      <li><s:property /></li>
   </s:iterator>
</ul>

</body>
</html>

接下來讓我們有employees.jsp包含以下內容:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employees</title>
</head>
<body>
   <p>An example of the include tag: </p>
   <s:include value="HelloWorld.jsp"/>
</body>
</html>

配置檔

你的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.zaixian.struts2.HelloWorldAction"
      method="execute">
      <result name="success">/HelloWorld.jsp</result>
   </action>
   <action name="employee"
      class="com.zaixian.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/hello.action。這會給出以下畫面:

Struts bean tag

在這個例子中,我們實例化一個新的org.apache.struts2.util.Counter bean實例。然後我們的第一個屬性設置為20和25最後一個屬性。這意味著計數器的值分別為20,21,22,23,24和25。我們給一個名為“Counter”的bean。 struts的bean標籤將bean實例化,並把它值棧中的。現在我們可以使用迭代器去,通過計數器bean的nd列印出計數器的值。.


上一篇: Struts2 include標籤 下一篇: Struts2快速入門