Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example
2.1.
Create Action Class
2.2.
Create views
2.3.
Configuration Files
3.
Frequently Asked Questions
3.1.
What are structs in JAVA?
3.2.
Write about Struts XML.
3.3.
What does struts2 mean in action?
3.4.
What is a set in java?
3.5.
What is an ORM tool?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Bean Tag

Author Manan Singhal
0 upvote

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.

Structs 2

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.

Frequently Asked Questions

What are structs in JAVA?

Structs is an open-source Java framework used for developing JAVA EE web applications. Many developers use it as it allows them to build maintainable, flexible, and extensible web applications faster. 

Write about Struts XML.

The struts.xml file contains the configuration information that will modify as actions are generated. For an application using this file, such as struts, DevMode = false, and other default choices provided in property files can be modified.

What does struts2 mean in action?

The action tag allows programmers to execute the action specified on the view page. They can do this by giving the activity a name. They can set the "executeResult" option to "true" to render the result directly in the view.

What is a set in java?

A data structure called a set is used in Java to contain distinct components. There can be no duplicates in a set collection. The equals() and hashCode() methods must be implemented for an element to be added to a set.

What is an ORM tool?

An object-relational mapping (ORM) tool makes it easier to create, manipulate, and store objects and makes them more accessible. It offers a means of translating programming languages (such as Python and Java) into objects that can be mapped to a SQL database.

Conclusion

In this article, we learned the structs 2 bean tag concept. We have also discussed the concept of the bean tag using an example.

I hope you would have gained a better understanding of these topics now!

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Thank You
Live masterclass