MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to access input parameters in Struts2

In this article, we will learn how to access input parameters into struts action class and also how to access input parameters into JSP files using struts framework.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Struts 2 is the web application framework that is used to create and manage web application. While a web application form is submitted, it is a tedious task to access input parameters of submitted form. Struts 2 framework provides a very simple way to access these input parameters of submitted form. It uses the java bean classes to access input parameters submitted by html form into struts action class and struts tag library is used to get back these input parameters values into JSP file.

Access Input Parameters into Struts 2 Framework:

When a html form is submitted, it transfers input data from client machine to server machine. On the server machine, we are required to access these input variables to manipulate according to its need. We are required to access these input variables into Struts Action classes and into JSP files.

In the struts action class, we access input variables using bean class. We are required to create class member variables with the same name as defined into the html form. Now create its getter() and setter() methods. The getter() methods will be used to access these values of input variables into our action classes and JSP files.

Directory Structure

Figure 1: Directory Structure

Figure 1 shows the directory structure created by maven for struts 2 web application.

Required Jar files to work with Struts 2 Framework

Figure 2: Required Jar files to work with Struts 2 Framework

Figure 2 displays the list of required jar files, which are required to run struts 2 web application.

Listing 1: Welcome.java

package com.test.login.action;

public class WelcomeUserAction{

	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String execute() {
		return "SUCCESS";
	}
}

Listing 1 defines a class WelcomeUserAction.java that defines two class member variable of type String with its getter() and setter() methods and also define a method execute() that return “Success” string. Struts make is very easy to access the data of submitted html form by just defining the same name of class member variables with html form input attributes.

Now let’s define struts.xml configuration file in Listing 2:

Listing 2: struts.xml

<?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>
	<package name="user" namespace="/User" extends="struts-default">
		<action name="Login">
			<result>login.jsp</result>
		</action>
		<action name="Welcome" class="com.test.login.action.WelcomeUserAction">
			<result name="SUCCESS">welcome.jsp</result>
		</action>
	</package>
</struts>

Listing 2 defines a sturst.xml xml configuration file that defines a namespace “User” with two action tags “Login” and “Welcome”. Login action loads login.jsp file and Welcome action loads welcome.jsp file according to WelcomeUserAction java class status.

Now define login.jsp file in Listing 3:

Listing 3: login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Login</title></head>
<body>
<h1>Struts 2 Input Parameter Access Login</h1>

<s:form action="Welcome">
	<s:textfield name="username" label="Username"/>
	<s:password name="password" label="Password"/>
	<s:submit/>
</s:form>

</body>
</html>

Listing 3 defines a html form using Struts tag library sets. This html form defines three attributes “username”, “password” and “submit” button. “username” is a text attribute and “password” is password attribute. This html form hits to “Welcome” struts action.

 Login Page

Figure 3: Login Page

Figure 3 shows the login form created by login.jsp file, while the project has been deployed on the application server. It accepts username and the password and forwards the input information to the “Welcome” action.

Now lets define welcome.jsp file into Listing 4:

Listing 4: welcome.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head></head>
<body>
<h1>Struts 2 Input Parameter Access Welcome</h1>

<h4>UserName : <s:property value="username"/></h4>
<h4>Password : <s:property value="password"/></h4>

</body>
</html>

Listing 4 defines a welcome.jsp file that reads input data from submitted html form using getter() methods of struts action class and displays the username and password into JSP file. It uses struts tag library property tag <s:property> to access its values, where “s” is a prefix of the tag to be used into the library tags.

Welcome Page

Figure 4: Welcome Page

Figure 4 displays the welcome.jsp view created after application deployment, when a login.jsp page is submitted. It displays username and password entered by the user into the login.jsp page.

Lets define web.xml deployment descriptor file into Listing 5:

Listing 5: web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>
  
  <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>

Listing 5 defines a xml file web.xml. It’s a deployment descriptor for web application that defines document type into its first tag and <web-app> tag defines <display-name> tag to define the application name and a filter dispatcher using <filter> tag. It defines <filter-name> and <filter-class> to get the filter details and rule of filter into “StrutsPrepareAndExecuteFilter” class of struts library. All the hitting urls on the application server are mapped to this class to be filtered and moved to struts.xml configuration file. <filter-mapping> and its sub tags <filter-name> and <url-pattern> are used to map all the urls to filter.

Now, Lets define pom.xml file into Listing 6 that is used to pull the directory structure from the maven repository:

Listing 6: pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong.common</groupId>
	<artifactId>Struts2Example</artifactId>
	<packaging>war</packaging>
	<version>com.mkyong.common</version>
	<name>Struts2Example Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.1.2</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>Struts2Example</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Listing 6 defines pom.xml file that pulls the directory structure for Struts 2 Web application. This file defines <modelVersion>, <groupId>, <artifactId>, <packaging>, <version>, <name> and <url> tags. These tags help maven to understand the type of archetype to pull from the maven repository. Maven also takes care of dependencies and required jar files set to be downloaded.

<dependencies> tag of pom.xml file defines dependencies and required jar files set definition, which are needed to be pulled from the repository. It defines Struts – Core library version 2.3.1.2 that means to download all the required jar files available for Struts version 2.3.1.2.

Maven is the build tool that also defines a plugin to compile and build this application, if not using any development IDE. It is defined in <build> tag and its sub – tags.

To make this archetype directory structure to work with Eclipse IDE, run the command below into the project directory. This command pull the set of required jar files and will make compatible with Eclipse IDE.

mvn eclipse:eclise

Conclusion:

In this article, we learn about Struts 2 working procedure to access input parameters from html form to struts action classes and JSP files. We learn about the methodology to access input variables into the struts action class and into the JSP files.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login