如何在自定义拦截器中使用应用程序范围变量?
How to use Application scope variable in custom interceptor?
我在访问应用程序映射时得到一个 NullPointerException
,因为我已经实现了 ApplicationAware
接口,所以应该注入拦截器。
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;
}
ApplicationAware 是一个接口,用于将全局映射 注入到操作 中,而不是拦截器:
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.
有多种方法可以实现您想要的,但我很确定这不是您需要的。为什么要将登录信息放入全局对象中?只需 写入和读取 Session,这就是您所需要的。
我在访问应用程序映射时得到一个 NullPointerException
,因为我已经实现了 ApplicationAware
接口,所以应该注入拦截器。
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;
}
ApplicationAware 是一个接口,用于将全局映射 注入到操作 中,而不是拦截器:
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.
有多种方法可以实现您想要的,但我很确定这不是您需要的。为什么要将登录信息放入全局对象中?只需 写入和读取 Session,这就是您所需要的。