Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

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

×

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

Accessing hidden values in Struts Framework

In this article, we will learn the reason to have hidden variables into our web applications. Also we will see the process to access hidden variables into Struts Web Applications.

In a web application, when an html or request form is being submitted. It contains a set of data variables that holds the data given by the user and by the hidden variables that are pre-set or assigned by JavaScript code. These attributes are sent via request from to the application or the web server. The struts framework provides a separate way to handle these values of user and hidden variables.

Accessing Hidden Values in Struts web application in Detail:

So, Struts provides a tag library that holds a set of tags. A tag <s:hidden> out of this set is used to set up a hidden variable into the web page. This variable is accessed using the setter() and getter() methods of the action class variables. An action class need to have the same class variable name with its getter() and setter() methods, those are used to define into the html form attributes.

Let’s go with an example that will setup a hidden variable to my website url value. This hidden variable will be accessed using the struts action class variables and will be displayed into another webpage. So, Listing 1 defines a jsp page “setHiddenValue.jsp” as:

Listing 1: setHiddenValue.jsp

<%@taglibprefix="s"uri="/struts-tags"%>
<html>
	<head>
		<title>Hidden Variable Initialize</title>
	</head>
	<body>
		<h1>Struts 2 - hidden value example</h1>
		<s:formaction="getHidden"namespace="/">
			<h4>This page has a hidden value (view source): 
			<s:hiddenname="url"value="http://www.umesh.asia"/></h4>
			<s:submitvalue="submit"name="submit"/>
		</s:form>
	</body>
</html>

Listing 1 defines a webpage that setup a hidden variable “myUrl” using struts tag library tag <s:hidden> with value attribute “https://www.umesh.asia” . One more attribute “Submit” button is defined using the tag <s:submit> with label “Submit”. These two attributes are enclosed using the html form. This form is defined using the struts tag library tag <s:form> that defines an action attribute to “helloHidden” with its namespace “/”.

Let’s define an action class that access the value of the hidden variable and loads this value into the webpage. So, Listing 2 defines HiddenValueAction.java class file as:

Listing 2: HiddenValueAction.java

packagecom.article.hidden.action;

import com.opensymphony.xwork2.ActionSupport;

publicclassHiddenValueActionextendsActionSupport{

	private String url;

	public String getUrl() {
		returnurl;
	}

	publicvoidsetUrl(String url) {
		this.url = url;
	}

	public String execute() {
		returnSUCCESS;
	}

}

Listing 2 defines an action class HiddenValueAction class that defines a string class type variable “myUrl” with its getter() and setter() methods. It also defines one more methods execute() that returns a string “SUCCESS”. This action loads a jsp page and displays the value of hidden variable into this as listed into Listing 3:

Listing 3: getHiddenValue.jap

<%@pagecontentType="text/html;charset=UTF-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<html>
	<head>
		<title>Display Hidden Values</title>
	</head>
	<body>
		<h1>Struts 2 Hidden Value Displayed</h1>
		<h4>
		  The hidden value :
		<s:propertyvalue="url"/>
		</h4>
	</body>
</html>

Listing 3 defines a jsp file “getHiddenValue.jsp” file displays the value of the hidden variable with the single scripted line. The hidden variable value is being accessed using <s:property> tag, where attribute “value” access the value of the hidden variable or the variables defined into an action class.

Now, let’s define a struts.xml configuration file that configures these jsp and action class files as listed into Listing 4:

Listing 4: struts.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<packagename="user"namespace="/"extends="struts-default">
		<default-action-refname="setHidden"/>
		<actionname="setHidden">
			<result>pages/setHiddenValue.jsp</result>
		</action>
		<actionname="getHidden"class="com.article.hidden.action.HiddenValueAction">
			<resultname="success">pages/getHiddenValue.jsp</result>
		</action>
	</package>
</struts>

Listing 4 defines a two <action> tags “setHidden” and “getHidden” into <package> tag. “setHidden” action loads a jsp file “setHiddenValues.jsp” file from “pages” directory and “getHidden” action loads “getHiddenValues.jsp” file from “pages” directory, if the java class “HiddenValueAction” returns success. The tag <default-action-ref> defines the server to load the action name “setHidden” as default action.

Now, let’s define web.xml file into Listng 5 that is the deployment descriptor file to manage the web application on the web server as:

Listing 5: web.xml

<!DOCTYPEweb-appPUBLIC
"-//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 web.xml, a deployment descriptor xml file. This file manages the web application and transmits all hitting requests from the web server to struts filter dispatcher class “StrutsPrepareAndExecteFilter”.

The directory structure for this web application is created using the maven repository that also resolve all the dependent jar files for this web application. The maven definition for this web application is defined into the pom.xml file that pulls the required directory structure and the set of required jar file from the maven repository as listed into Listing 6:

Listing 6: pom.xml

<projectxmlns="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 MavenWebapp</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>Struts2HiddenValueExample</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 a set of tags that declares the information about the artifact that needs to pull from the maven repository and also defines the dependent jar files with its version that will help to deploy and run this application into the web browser. This example is created using Struts 2.3.2 version and Struts 2.3.2 version jar files are required to run this web application.

Once this web application has been deployed on the web or application server, visit the url into the web browser as: http://localhost:8080/Struts2Example/

This url will loads the default action “setHidden” as pre-setup into the struts.xml configuration file and will look like as shown in Figure 1:

setHidden.action loaded into browser

Figure 1: setHidden.action loaded into browser

When the application is deployed, it loads the setHidden.action by-default, Once the submit button of the web page is clicked, it loads an action “getHidden.action” as shown into the figure 2:

Value displayed of Hidden variable into getHidden.action

Figure 2: Value displayed of Hidden variable into getHidden.action

Figure 2 displays the value of the hidden variable that was setup into the setAction.action.

Required Tools & Application:

There is a set of required application and tools listed to run this application:

  • Eclipse IDE (Eclipse JUNO)
  • JDK Environment (JDK 1.7)
  • Application or Web Server (Tomcat Apache 7)
  • Set of Struts 2 Jar Files shown into the figure

Conclusion:

In this article, we learn about the need of using hidden variables into the web application and also learn the way of using hidden variables into our web application. This article defines a struts 2 example to access hidden variable defined into the web application.



Working in Software Development domain from 7 years now and is well equipped with programming languages like HTML, CSS, Java, PHP, .NET etc.

What did you think of this post?
Services
[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