Struts 2 - The Bean Tag

Il tag bean è una combinazione di set e pushtag, consente di creare una nuova istanza di un oggetto e quindi impostare i valori delle variabili. Quindi rende disponibile il bean nell'archivio valori, in modo che possa essere utilizzato nella pagina JSP.

Il tag Bean richiede un java bean con cui lavorare. Quindi, dovrebbero essere seguite le leggi standard sui java bean. Cioè, il bean dovrebbe avere un costruttore senza argomenti. Tutte le proprietà che vuoi esporre e usare dovrebbero avere i metodi getter e setter. Ai fini di questo esercizio, utilizziamo la seguente classe Counter contenuta nel pacchetto struts util. La classe Counter è un bean che può essere utilizzato per tenere traccia di un contatore.

Cerchiamo di mantenere tutti i file invariati e modificare il file HelloWorld.jsp.

Crea classe d'azione

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

Crea viste

Facciamo HelloWorld.jsp con il seguente contenuto -

<%@ 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>

Avanti lasciaci employees.jsp con il seguente contenuto -

<%@ 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>

File di configurazione

Il tuo struts.xml dovrebbe apparire come -

<?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>

Il tuo web.xml dovrebbe apparire come -

<?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>

Fare clic con il tasto destro sul nome del progetto e fare clic Export > WAR Fileper creare un file War. Quindi distribuire questo WAR nella directory webapps di Tomcat. Infine, avvia il server Tomcat e prova ad accedere all'URLhttp://localhost:8080/HelloWorldStruts2/hello.action. Questo produrrà la seguente schermata:

In questo esempio, creiamo un'istanza di una nuova istanza del bean org.apache.struts2.util.Counter. Quindi impostiamo la prima proprietà su 20 e l'ultima proprietà su 25.

Ciò significa che il contatore avrà i valori 20,21,22,23,24 e 25. Diamo al bean un nome "contatore". Il tag bean struts istanzia il bean e lo inserisce nello stack di valori. Ora possiamo usare l'iteratore per passare attraverso il bean Counter e stampare il valore del contatore.