C#

A Struts Application for Form Handling

The following article describes A Struts Application for Form Handling.

In order to understand how a struts application works with a form, we create an application for the same. The following problem statement, we use here.

An HTML form for a Cultural Event accepts these fields: Team title, event name like solo singing, group singing, solo dance, group dance or band performance, and team size. Create the HTML form and develop a struts application to perform form handling.

Implement A Struts Application for Form Handling

The following code shows an example of the HTML form for the above problem.

<!DOCTYPE html>
<html>
<head>
	<title>Cultural Event Form</title>
</head>
<body>
	<h1>Register for the Cultural Event</h1>
	<form action="submitForm.do" method="post">
		<label for="teamTitle">Team Title:</label>
		<input type="text" id="teamTitle" name="teamTitle" required><br>

		<label for="eventName">Event Name:</label>
		<select id="eventName" name="eventName" required>
			<option value="">Select an Event</option>
			<option value="soloSinging">Solo Singing</option>
			<option value="groupSinging">Group Singing</option>
			<option value="soloDance">Solo Dance</option>
			<option value="groupDance">Group Dance</option>
			<option value="bandPerformance">Band Performance</option>
		</select><br>

		<label for="teamSize">Team Size:</label>
		<input type="number" id="teamSize" name="teamSize" required><br>

		<input type="submit" value="Submit">
	</form>
</body>
</html>

As can be seen, this HTML form contains three fields – teamTitle, eventName, and teamSize – and a submit button. Furthermore, The eventName field is a dropdown menu that allows the user to select one of the predefined event names.

In order to handle this form in a Struts application, follow these steps.

  1. At first, Set up the development environment and create a new Struts project.
  2. Then, define the JavaBean class to hold the form data.
  3. After that, create a JSP page that displays the HTML form and binds it to the JavaBean properties.
  4. Create a Struts action class that processes the form data.
  5. Then, map the action to the form submission URL in the struts-config.xml file.
  6. Further, implement the execute method in the action class to retrieve the form data from the JavaBean, perform any necessary processing, and set any output data on the request or session.
  7. Finally, create a JSP result page that displays the result of the form submission.
  8. Once, the above steps are complete, deploy the application to a web server and test it by submitting the form and verifying that the result is displayed correctly.
  1. Create a web page in JSP, that implements the above form. For example.
<!DOCTYPE html>
<html>
<head>
	<title>Cultural Event Form</title>
</head>
<body>
	<h1>Register for the Cultural Event</h1>
	<form action="submitForm.do" method="post">
		<label for="teamTitle">Team Title:</label>
		<input type="text" id="teamTitle" name="teamTitle" required><br>

		<label for="eventName">Event Name:</label>
		<select id="eventName" name="eventName" required>
			<option value="">Select an Event</option>
			<option value="soloSinging">Solo Singing</option>
			<option value="groupSinging">Group Singing</option>
			<option value="soloDance">Solo Dance</option>
			<option value="groupDance">Group Dance</option>
			<option value="bandPerformance">Band Performance</option>
		</select><br>

		<label for="teamSize">Team Size:</label>
		<input type="number" id="teamSize" name="teamSize" required><br>

		<input type="submit" value="Submit">
	</form>
</body>
</html>
  1. After that, create a JavaBean Class for this application.

The JavaBean class will represent the form data. So, in this example, we will call it CulturalEventForm. Also, it will have three properties – teamTitle, eventName, and teamSize – with their corresponding getter and setter methods.

public class CulturalEventForm {
    private String teamTitle;
    private String eventName;
    private int teamSize;
 
    public String getTeamTitle() {
        return teamTitle;
    }
 
    public void setTeamTitle(String teamTitle) {
        this.teamTitle = teamTitle;
    }
 
    public String getEventName() {
        return eventName;
    }
 
    public void setEventName(String eventName) {
        this.eventName = eventName;
    }
 
    public int getTeamSize() {
        return teamSize;
    }
 
    public void setTeamSize(int teamSize) {
        this.teamSize = teamSize;
    }
}
  1. Then, create Struts Action Class.

In fact, the Struts action class will handle the form submission and any necessary processing. So, in this example, we will call it CulturalEventAction. It will extend the org.apache.struts.action.Action class and override the execute() method. This method will retrieve the form data from the CulturalEventForm, perform any processing, and set any output data on the request or session.

public class CulturalEventAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
 
        CulturalEventForm eventForm = (CulturalEventForm) form;
 
        String teamTitle = eventForm.getTeamTitle();
        String eventName = eventForm.getEventName();
        int teamSize = eventForm.getTeamSize();
 
        // perform any processing needed
 
        request.setAttribute("teamTitle", teamTitle);
        request.setAttribute("eventName", eventName);
        request.setAttribute("teamSize", teamSize);
 
        return mapping.findForward("success");
    }
}
  1. struts-config.xml

The struts-config.xml file will map the form submission to the CulturalEventAction class and define the forward to the success page. For example.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
    "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean name="culturalEventForm" type="com.example.CulturalEventForm"/>
    </form-beans>
 
    <action-mappings>
        <action path="/submitForm" name="culturalEventForm" type="com.example.CulturalEventAction">
            <forward name="success" path="/success.jsp"/>
        </action>
    </action-mappings>
</struts-config>
  1. JSP Result Class

The following JSP could be used in conjunction with the Struts action class and struts-config.xml file. This JSP will display the data that was submitted in the form and processed by the Struts action class.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cultural Event Result</title>
</head>
<body>
    <h1>Cultural Event Result</h1>
 
    <p>Team Title: <c:out value="${teamTitle}" /></p>
    <p>Event Name: <c:out value="${eventName}" /></p>
    <p>Team Size: <c:out value="${teamSize}" /></p>
 
</body>
</html>

This JSP uses the JSTL core tag library to display the form data. It retrieves the values of the teamTitle, eventName, and teamSize attributes that were set in the CulturalEventAction class and displays them using the <c:out> tag. The ${...} syntax is used to access the values of the attributes.

In fact, this is a basic example of how the code for a Struts application that handles a cultural event form might look like. Keep in mind that the implementation details will depend on the requirements of your specific application.



Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *