struts2中action中取不到session的值
请问高手,struts2中action中取不到session的值。
代码如下:
java代码如下:
public class LoginAction extends BaseAdminAction {
getRequest().getSession().setAttribute(SystemUtil.USERINFO, user);
ActionContext.getContext().getSession().put("dishesPM","dishesPM");
System.out.println("================LoginAction.checkLogin() USERINFO/dishesPM set=============="+getRequest().getSession().getId());
return ajax(Status.success, "登录成功");
}
public class IndexAction extends BaseAdminAction {
private static final long serialVersionUID = 12L;
public String index(){
String test = (String)getRequest().getSession().getAttribute("dishesPM");
//System.out.println(test+"===========IndexAction.list() dishesPM get================="+getRequest().getSession().getId());
test = (String)ActionContext.getContext().getSession().get("dishesPM");
System.out.println(test+"===========IndexAction.list() dishesPM get================="+getRequest().getSession().getId());
return "index";
}
}
基类写了几个公共的方法如下:
public class BaseAdminAction extends ActionSupport {// 获取Request
// 获取Request
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
// 获取Response
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
// 获取ServletContext
protected ServletContext getServletContext() {
return ServletActionContext.getServletContext();
}
// 获取Attribute
protected Object getAttribute(String name) {
return ServletActionContext.getRequest().getAttribute(name);
}
// 设置Attribute
protected void setAttribute(String name, Object value) {
ServletActionContext.getRequest().setAttribute(name, value);
}
// 获取Parameter
protected String getParameter(String name) {
return ServletActionContext.getRequest().getParameter(name);
}
// 获取Parameter数组
protected String[] getParameterValues(String name) {
return ServletActionContext.getRequest().getParameterValues(name);
}
// 获取Session
protected Object getSession(String name) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
return session.get(name);
}
// 设置Session
protected void setSession(String name, Object value) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.put(name, value);
}
// 移除Session
protected void removeSession(String name) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.remove(name);
}
}
struts.xml文件
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="xhtml"/> <!-- .xhtml为访问扩展名 -->
<constant name="struts.convention.action.name.separator" value="_" />
<constant name="struts.convention.result.path" value="/jsps/" />
<constant name="struts.convention.classes.reload" value="false" />
<constant name="struts.convention.action.packages" value="com.define.dishes.action" />
<constant name="struts.objectFactory" value="spring" />
<package name="basePackage" extends="struts-default">
<interceptors>
<interceptor-stack name="baseStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="multiselect" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo..*,^struts..*,.*\u0023.*,.*\x5Cu0023.*,.*\x5cu0023.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="baseStack" />
<default-action-ref name="errorPage404Action" />
<action name="errorPage404Action">
<result type="redirect">/commons/error_page_404.html</result>
</action>
</package>
<package name="admin" extends="basePackage" namespace="/admin">
<interceptors>
<interceptor name="logInterceptor" class="com.define.dishes.interceptor.LoginedCheckInterceptor" />
<interceptor name="trimInterceptor" class="com.define.dishes.interceptor.TrimInterceptor" />
<interceptor-stack name="adminStack">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-ms-bmp,image/png,image/x-png</param>
<param name="maximumSize">2097152</param>
</interceptor-ref>
<interceptor-ref name="baseStack" />
<interceptor-ref name="logInterceptor" />
<interceptor-ref name="trimInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="adminStack" />
<global-results>
<result name="main" >/jsps/admin/main.jsp</result>
<result name="login" >/jsps/admin/login.jsp</result>
<result name="index" >/jsps/admin/index.jsp</result>
<result name="top" >/jsps/admin/top.jsp</result>
<result name="left" >/jsps/admin/left.jsp</result>
</global-results>
</package>
以上是主要的代码。
现在loginaction 转到的main.jsp可以取到session的值,但main.jsp进入到indexAction.action中就取不到session的值。所有的action类都一样,经过页面进来的action都不能取到session的值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
main.jsp 看看