使用struts2,使用SessionAware将userObject保留在会话中,我发现在MainAction执行期间userObject在会话中正确设置,但是每次返回null时在另一个action(UploadAction)类中的不同流程中获取userObject。检查了很多东西但没有用。我是 struts 和 SessionAware 的新手,我在这里缺少什么?将所有相关代码放在这里。
public class WelcomeAction extends MainAction {
private UserObject userObject = new UserObject();
public String executeAction() {
userObject = //DB CALL;
this.getSession().put("user", userObject);
System.out.println("++"+this.getSession().get("user"));
//giving me UserObject
}
}
public class MainAction extends ActionSupport implements SessionAware{
static Map<String,Object> session;
public MainAction() {
}
public Map<String,Object> getSession() {
return session;
}
@Override
public void setSession(Map<String,Object> session) {
BaseAction.session = session;
}
}
public class UploadAction extends MainAction implements ServletRequestAware{
public String executeAction() {
UserObject uobj = (UserObject) this.getSession().get("user");// this line always give me null;
//this uobj always null
}
}
struts.xml
<interceptors>
<interceptor name="sessionInterceptor"
class="***.SessionInterceptor"></interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="sessionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loggingStack"></default-interceptor-ref>
<action *******.WelcomeAction
method="executeAction">
<result name="success" type="redirectAction">*******</result>
</action>
<interceptors>
<interceptor name="logInterceptor" class="*********LogInterceptor"></interceptor>
<interceptor name="sessionInterceptor" class="*********.SessionInterceptor"></interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="sessionInterceptor" />
<interceptor-ref name="logInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loggingStack"></default-interceptor-ref>
<action name="upload"
class="*******.UploadAction"
method="executeAction">
<interceptor-ref name="Upload">
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success" type="json">
<param name="noCache">true</param>
<param name="excludeNullProperties">false</param>
<param name="contentType">text/html</param>
<param name="includeProperties">sessionId.*,lstError.*,success.*,message.*</param>
</result>
<result name="input" type="json">
<param name="noCache">true</param>
<param name="excludeNullProperties">false</param>
<param name="contentType">text/html</param>
<param name="includeProperties">success.*,message.*</param>
</action>
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern></url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>*******</servlet-name>
<servlet-class>*******.Process******
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
SessionInterceptor::
if ( !className.equalsIgnoreCase("WelcomeAction")
&& !className.equalsIgnoreCase("com.opensymphony.xwork2.ActionSupport")
&& ( session == null || session.isEmpty()) ) {
return "session";
} else {
return actionInvocation.invoke();
}
LogInterceptor
Map session = (Map)ActionContext.getContext().get("session");
UserObject userObject = this.getSession().get("user"));
if(userObject==null){
return "session";
}
else{
}
String result = invocation.invoke();
您已使用不同的属性将会话对象注入到操作中。一个是
MainAction
的属性,另一个是 BaseAction
中的属性。当您调用 this.getSession()
时,它会从 session
返回一个 MainAction
,但是当注入会话对象时,属性为 BaseAction
。
您应该更改 setter 方法,以便在注入时将会话对象分配给
MainAction
@Override
public void setSession(Map<String,Object> session) {
this.session = session;
}
不要使用操作的
static
属性。
还要正确实施
SessionAware
,您可以阅读this答案。
您还创建了两个
interceptor-stack
,名称为 loggingStack
,其中一个覆盖另一个。