Back to: Java Struts Tutorials
Struts 2 Bean and Param Tags with Examples
In this article, I am going to discuss Struts 2 Bean and Param Tags with Examples. Please read our previous article where we discussed Action Tag in Struts 2.
What is Bean Tag in Struts 2?
In Struts, the bean tag combines the functionalities of the set and push tags, offering a convenient way to create a new object instance and set its variable values. This resulting bean object becomes accessible within the ValueStack, making it readily available for utilization on the JSP page.
To make use of the Bean tag effectively, it requires a Java bean that adheres to the standard conventions. This implies that the bean should have a parameterless constructor, and all the properties you intend to utilize must have corresponding getter and setter methods. To illustrate this, let’s consider the Counter class from the struts util package as an example.
The Counter class can serve as a bean, which is suitable for our purposes in this context. It is designed to maintain and track a counter, providing a straightforward illustration of how the bean tag can be employed.
By using the bean tag, we can instantiate the Counter class and set its initial value. For instance, if we want to start the counter at zero, we would set the value accordingly. Then, as the user interacts with the application or certain events occur, we can utilize the bean tag to manipulate the counter’s value, incrementing or decrementing it as needed.
With the updated counter value stored in the bean object within the valuestack, we can easily access it on our JSP page. This enables us to display the current count, use it in conditional statements, or perform any other operations required for the application’s functionality.
In summary, the bean tag in Struts is a powerful tool that simplifies the creation and manipulation of Java bean objects. By following standard Java bean conventions, we can make use of the bean tag to manage and interact with data effectively in our Struts-based web applications. The Counter class provides a practical example of how this tag can be employed to keep track of a counter and utilize it on the JSP page for a variety of purposes.
What is Param Tag in Struts 2?
The param tag plays a crucial role in parameterizing different tags within a programming or markup context. It allows us to customize the behavior of these tags by passing specific parameters to them. Two prime examples of tags that can benefit from parameterization are the include tag and the bean tag.
When we use the param tag, we can dynamically modify the behavior of the include and bean tags by passing relevant parameters. For instance, let’s consider a scenario where we have previously discussed the bean tag, which represents an object in a Java-based application.
Suppose we have a bean tag that defines a car object with properties like “make,” “model,” and “year.” By utilizing the param tag, we can easily customize this bean tag to represent different cars with varying attributes. We can pass parameters such as “make” = “Toyota,” “model” = “Camry,” and “year” = “2023” to create a specific instance of the car object.
Similarly, when using the include tag, the param tag empowers us to include files or templates while providing specific values to variables within those files. This capability allows for modularization and reusability of code, making our programming more efficient and maintainable.
In conclusion, the param tag serves as a powerful tool to parameterize tags like include and bean, allowing developers to customize and adapt the behavior of these tags based on specific requirements. It enhances flexibility and code reusability, leading to more effective and organized software development.
How to Implement Bean and Param Tags in Struts 2?
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Modify pom.xml to add the required dependencies and the build configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Step 3: In the src/main/webapp directory, create a new file called index.jsp. This file displays the different types of cars on the webpage. Add the following content to the file:
<%@ page contentType = "text/html; charset = UTF-8"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <html> <head> <title>Bean tag demo</title> </head> <body> <s:bean name = "org.apache.struts2.util.Counter" var = "counter"> <s:param name = "first" value = "10"/> <s:param name = "last" value = "20" /> </s:bean> <ul> <s:iterator value = "counter"> <li><s:property /></li> </s:iterator> </ul> </body> </html>
Step 4: In the src/main/java directory, create a new file called HelloWorldAction.java. The file contains the Java action code. Add the following content to the file:
package com.dotnet; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String name; public String execute() { return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Step 5: In the src/main/webapp directory, create a new file called struts.xml. This file is responsible for configuring struts2. Add the following content to the file:
<?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 = "HelloWorldAction" class = "com.dotnet.HelloWorldAction" method = "execute"> <result name = "success">/index.jsp</result> </action> </package> </struts>
Step 6: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
<?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/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</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.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Step 7: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
Congratulations! You now know how to implement bean tags in struts2!
In the next article, I am going to discuss Struts 2 Bean and Param Tags with Examples. Here, in this article, I try to explain Struts 2 Bean and Param Tags with Examples and I hope you enjoy this Struts 2 Bean and Param Tags article.