Struts 2 bean tag

The bean tag in struts is used to set values of the variable by creating the new instance of an object. It allows you to set the values and then push them. This bean is then available in valuestack and can be further used anywhere on any jsp page where required. The bean tag in struts is mainly used to instantiate the Bean class on jsp page. To set values of properties of bean class, param is used. Name can be assigned to a bean using the var attribute.

Struts Data tags- Bean tag Example:

Create the index.jsp file as first jsp file:

The index.jsp page contains the heading Bean Tag Example and Roll numbers of Students. Using org.apache.struts2.util.Counter, we have created the new bean with the first property set to 1 and the last one to 10. We can use this bean anywhere in any other resource. Using iterator we can print values of the bean.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <title> Index Page </title>
   </head>
   
   <body>
     <h3> Bean Tag Example  </h3>
      <h3> Roll numbers of Students: </h3>
       
      <s:bean name = "org.apache.struts2.util.Counter" var = "counter">
         <s:param name = "first" value = "1"/>
         <s:param name = "last" value = "10" />
      </s:bean>
      
      <ul>
         <s:iterator value = "#counter">
            <li><s:property /></li>
         </s:iterator>
      </ul>
      
   </body>
</html>

Create student.jsp file to include index.jsp file:

The student.jsp page is used to include output of other page in student page. It shows content of student.jsp page with output of index.jsp file that is instance of bean showing values from 1 to 10 as roll number of students is included in it.

<%@ page language ="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding ="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">




<html>
   <head>
      <title> Student page </title>
   </head>
   
   <body>
      <h3> This is the student page with roll number from the index.jsp page: </h3>
      <s:include value = "index.jsp"/>
   </body>
</html>

Create the class to include execute method:

The Demo class contains execute() method which returns a success string if all the things are executed properly.

public class Demo 
{ 


public String execute() throws Exception 
{
      return "success";
}
}

Construct struts.xml file:

In the struts.xml file, make the entry of the action class and link for it and result pages. The result determines what browser will display after the execution of the action. Results have optional names like success and error. We have made the entry of two Actions namely hello and student and called different jsp pages on them.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">




      <action name = "hello" 
         class = "Demo" 
         method = "execute">
         <result name = "success"> /index.jsp </result>
      </action>
      
      <action name = "student" 
         class = "Demo" 
         method = "execute">
         <result name = "success"> /student.jsp </result>
      </action>




   </package>
</struts>

web.xml file is created inside WEB-INF folder in WebContent folder:

The web.xml file in the WEB-INF folder specify how elements are processed. The entry of FilterDispatcher is done in the web.xml file. /* specifies all urls will be parsed. This task is done by struts filter.

<?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" 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> StrutsBean  </display-name>
  <welcome-file-list>
    <welcome-file> index.html </welcome-file>
    <welcome-file> index.htm </welcome-file>
    <welcome-file> index.jsp </welcome-file>
    <welcome-file> default.html </welcome-file>
    <welcome-file> default.htm </welcome-file>
    <welcome-file> default.jsp </welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>  struts2  </filter-name>
    <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>
  </filter>
  <filter-mapping>
    <filter-name> struts2 </filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Output:

In order to run the application, right-click on the project -> Click on the option Run As -> then select Run on Server. After that run the url, http://localhost:8080/StrutsBean/student.action. It will show the content of the student.jsp page, This is the student page with roll number from the index.jsp page. It includes the content of the index.jsp page that is headings,  Bean Tag Example, and Roll numbers of Students followed by 1 to 10 roll numbers. As we have created bean with the first property set to 1 and the last property set to 10, it will show the list of roll numbers from 1 to 10. If we change the first and last property values we can increase or decrease the range. Once the bean is created we can use it in any other resource in struts web application.

Struts Data tags- Bean tag