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 .:: | ||
Comments
8 Responses to “Example on ApplicationAware Interface of struts 2”
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)
@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..!!
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
@Abhishek
Make sure you are writing #application.keys in your map object.
Download the program and check again and lemme know.
what is Value stack, Action context,and Action Invocation in struts2?
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?
Thanqqqqqqqqqqqqqqqqq its simply supeb to learn
Thank you… nice..!