Harshdeep Singh Harshdeep Singh - 1 month ago 8
Java Question

How to use Application scope variable in custom interceptor?

I'm getting a

NullPointerException
while accessing the application map that should've been injected in the Interceptor since I've implemented the
ApplicationAware
interface.

public String intercept(ActionInvocation ai) throws Exception {
String result = "expire";
app.put("login_msg", "Session Expired");
Map session = ai.getInvocationContext().getSession();

try {
if ((session != null && session.get("login").toString().equalsIgnoreCase("true")) || !app.get("db_name").toString().equals("")) {
log.info("login hai");
app.put("login_msg", "");
result = ai.invoke();
} else {
log.error(" LOGIN EXPIRED ");
}
} catch (NullPointerException npe) {
log.error("Error : " + npe);
}

log.info("Interceptor Result : " + result);
return result;
}

@Override
public void setApplication(Map<String, Object> map) {
app = map;
}

Answer

ApplicationAware is an interface used to inject a global map into actions, not interceptors:

Actions that want to be aware of the application Map object should implement this interface. This will give them access to a Map where they can put objects that should be available to other parts of the application.

There are mutliple ways to achieve what you want, but I'm pretty sure it's not what you need. Why on earth are you putting login informations into a global objects ? Just write to and read from the Session, that's what you need.

Comments