Struts 2 - Bean 標籤



bean 標籤是setpush 標籤的組合,它允許您建立一個物件的新例項,然後設定變數的值。然後它使 bean 在值棧中可用,以便可以在 JSP 頁面中使用它。

Bean 標籤需要一個 Java Bean 才能一起使用。因此,應遵循標準的 Java Bean 規則。即,bean 應該有一個無參建構函式。所有您想要公開和使用的屬性都應該具有 getter 和 setter 方法。出於本練習的目的,讓我們使用 struts util 包中提供的以下 Counter 類。Counter 類是一個 bean,可用於跟蹤計數器。

讓我們保持所有檔案不變並修改 HelloWorld.jsp 檔案。

建立 Action 類

package com.tutorialspoint.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.tutorialspoint.struts2.HelloWorldAction" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
      </action>
      
      <action name = "employee" 
         class = "com.tutorialspoint.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>

右鍵單擊專案名稱,然後單擊匯出 > WAR 檔案以建立 WAR 檔案。然後將此 WAR 部署到 Tomcat 的 webapps 目錄中。最後,啟動 Tomcat 伺服器並嘗試訪問 URL https://:8080/HelloWorldStruts2/hello.action。這將生成以下螢幕:

Struts bean tag

在此示例中,我們正在例項化 org.apache.struts2.util.Counter bean 的新例項。然後我們將第一個屬性設定為 20,最後一個屬性設定為 25。

這意味著計數器將具有值 20、21、22、23、24 和 25。我們為 bean 指定名稱“counter”。Struts bean 標籤例項化 bean 並將其放入值棧中。現在,我們可以使用迭代器遍歷 Counter bean 並打印出計數器的值。

struts_data_tags.htm
廣告

© . All rights reserved.