Introduction
By combining the set and push tags, the bean tag enables you to create an instance of an object and set the values of its variables. The bean is then made accessible in the value stack so the JSP page can use it.
A Java bean is necessary for the Bean tag to function. Consequently, the bean should have a function Object() with no arguments. Getter and setter methods should be available for all properties you intend to use and expose. Let's utilize the Counter class from the struts util package for the duration of this example.
Example
Create Action Class
package com.coding.ninjas.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;
}
}
Create views
Create a file with the name HelloWorld.jsp and put the content below.
<%@ 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>
Create a file with the named employee.jsp and put the below content into it.
<%@ 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>
Configuration Files
Create a file with the name struts.xml and put the below content into it.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC>
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "hello"
class = "com.coding.ninjas.struts2.HelloWorldAction"
method = "execute">
<result name = "success">/HelloWorld.jsp</result>
</action>
<action name = "employee"
class = "com.coding.ninjas.struts2.Employee"
method = "execute">
<result name = "success">/employee.jsp</result>
</action>
</package>
</struts>
Create a file with the name web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
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>
To produce a War file:
- Perform right-click on the project name and select Export > WAR File.
- Deploy this WAR to the webapps directory of Tomcat.
- Launch the Tomcat server, then attempt to access it.