Newsletter

Example on ApplicationAware Interface of struts 2

Struts » on Oct 24, 2011 { 8 Comments } By Sivateja

So ApplicationAware interface, we need to implement our Action class from ApplicationAware interface when ever our Action class need to get context behavior, means we can share our data across all the files of the web application by putting in a global object that’s context 🙂

When we implement our Action class from ApplicationAware interface then the controller doesn’t inject exactly servlet context object, instead it will injects a map object and this will created once by the controller and the same object will be injected to all files of the struts application.

Example on struts 2 ApplicationAware

files we used…

  • index.jsp
  • error.jsp
  • success.jsp
  • LogingEx.java [ in java4s package ]
  • web.xml [ in web-inf ]
  • struts.xml [ in web-inf/classes folder ]

Directory Structure

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>

<s:form action="verify">

    <s:textfield name="uname" label="Enter Username" /><br>
    <s:textfield name="age" label="Enter Age" /><br>
    <s:textfield name="country" label="Enter Country" /><br>

    <s:submit value="Click" align="center" /> 

</s:form>
</body>
</html>

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

Hello <s:property value="#application.a" />
Your age is:  <s:property value="#application.b" />
country:  <s:property value="#application.c" />

LogingEx.java

package java4s;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.*;
import java.util.Map;

public class LogingEx extends ActionSupport implements ApplicationAware{
	private static final long serialVersionUID = 1L;

	private String uname,country;
	private int age;
	Map m;

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setApplication(Map m)
	{
		this.m=m;
	}

	public String execute()
	{
		m.put("a",uname);
		m.put("b", age);
		m.put("c",country);

		return SUCCESS;
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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>
    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

After Execution

Output

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

8 Responses to “Example on ApplicationAware Interface of struts 2”
  1. Mohammed Vaseem says:

    Hello,
    Am getting exception in this program. Usually the exception messages will be displayed on the console and as well as on the browser. But am seeing this first time that, the exception messages are displaying on the browser but in console screen alike messages are displayed. Please provide the reason.

    When I was tracing the program, I got to know exception was coming at line 44 of LogingEx.java program.

    The browser exception messages are
    java.lang.NullPointerException
    com.dss.LogingEx.execute(LogingEx.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

  2. Java4s says:

    @Vaseem

    Its working fine from my end,any ways try these…

    – Make sure you download the program [ Click on Download button ]
    – If you copy the source code from the web page, while pasting the code it will prints some other characters in place of quotes.
    – If you copy, just remove all the double quotes and insert again manually.

    try finally..!!

  3. Abhishek says:

    Hey when i run this demo it will not show proprety value of my successfull pgae please help me where i am getting wrong in this

  4. Java4s says:

    @Abhishek

    Make sure you are writing #application.keys in your map object.

    Download the program and check again and lemme know.

  5. sanjeev kumar says:

    what is Value stack, Action context,and Action Invocation in struts2?

  6. Raghu says:

    Sir,

    This Program is very useful to get knowledge about applicationaware interface.But what is the difference between applicationaware and session?

    1.Can i use applicationaware instead of session to store User infomation on login session?

  7. asif says:

    Thanqqqqqqqqqqqqqqqqq its simply supeb to learn

  8. Lova Chittumuri says:

    Thank you… nice..!

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.