这是我的毕业设计,我和我的搭档是用HSS三大框架都用上了---------
-------------------所有的Action都继承这个类-----------------------------------------------
public class CommonAction extends ActionSupport implements SessionAware, ServletRequestAware ,ServletResponseAware{
private static final long serialVersionUID = 1L;
protected HttpServletRequest request;
protected Map<String, Object> session;
protected HttpServletResponse response;
/**
 * 将信息存入context区域,页面取值可使用<s:property value="#key">显示
 * 
 * @param key
 * @param msg
 */
protected void setMessage(String key, String msg) {
System.out.println("aaaa");
ActionContext.getContext().put(key, msg);
}


public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public Map<String, Object> getSession() {
return session;
}
@Override
public void setSession(Map<String, Object> arg0) {
session = arg0;
System.out.println(session.hashCode()); } public void setServletRequest(HttpServletRequest arg0) {
request = arg0;
}
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}}---------------------在LoginAction里面将user放进去-----------------------------------public String login(){
try {
Users user1=(Users)userService.login(user.getRegName(),DegistUtil.md5(user.getRegPassword()));
if(user1==null){
System.out.println("用户名密码错误");
request.setAttribute("login_error", "用户名或密码不正确");
return "error";

}else{
System.out.println("登入成功");
session.put("login_user", user1);
System.out.println(session.hashCode()+"ccc");
return "success";
}
} catch (Exception e) {
// TODO Auto-generated catch block
request.setAttribute("login_error", "系统繁忙,请稍后再试");
e.printStackTrace();
return "error";
}


}
----------------------------------------------------------------------------------------
然后 我在JSP页面上   <s:property value="#session['login_user'].regUsername" />   
-----------------------------------------------------------------------------------------
struct.xml里面的文件<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-admin.xml" />
<include file="struts-image.xml" />
<include file="struts-main.xml" />
<include file="struts-user.xml"></include>
<constant name="strusts.ObjectFactory" value="spring" />
<constant name="struts.ObjectFactory.spring.autoWire" value="name" /> <package name="product-default" extends="struts-default,json-default">

</package>
</struts>    
其他xml文件都   <package name="product-main" namespace="/main" extends="product-default">-----------------------------------------------------------------------------
现在  出现的问题是有时候会出现  有时候又会没有,我百度了一下类似的情况:struts中的拦截器使得session失效,我是个初学者,碰到这样的问题郁闷死了,希望各位大侠能指条明路!!!

解决方案 »

  1.   

    web.xml                                                                               ----------------------------------------------------------------------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
       <context-param>
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <filter>
       <filter-name>struts2</filter-name>
       <filter-class>
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
       </filter-class>
      </filter>
      <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>
      

  2.   


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
       <context-param>
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <filter>
       <filter-name>struts2</filter-name>
       <filter-class>
       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
       </filter-class>
      </filter>
      <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>
      

  3.   

    jsp页面取session的值不是这样取吧
    你把你这句话
    <s:property value="#session['login_user'].regUsername" />   
    换成EL表达式
    ${sessionScope.login_user.regUsername}试试
      

  4.   

    登入后 我打印session.hashCode有值,后面的action里面  我打印session.hashCode都是0;
      

  5.   

    我现在  发现  如果 那些都在../user/下面的session使用正常  如果跳到../main/里面去  session就变0了
      

  6.   

    把xml里面的命名空间都去掉吧  namespace="/main" 再重启下试试
      

  7.   

    是这样的,我曾经碰到过这类问题,我不知道你使用的是不是Struts2,我使用的是Struts2,我的问题是在Spring配置Action的时候
    <bean id="YonghuAction" class="cn.com.web.YonghuAction" scope="session">
    加入了scope="session",还可以加入其他的,你可以看看这方面的资料。不知道是不是你遇到的问题。
      

  8.   

    这个问题蛮奇怪的至今没碰到过,除非你不在一个web项目下 不然命名空间也不会改变session的作用于啊 要不我给你个配置按照我的方式配下web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
    <!-- Struts2的核心Action分发器 -->
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter> <!-- Stuts2核心分发器的一个辅助过滤器 -->
    <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.ActionContextCleanUp
    </filter-class>
    </filter> <!-- 编码转换 过滤器 -->
    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
    org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>  
            <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> <filter-mapping>
    <filter-name>struts2</filter-name> 
            <url-pattern>/*</url-pattern> 
            <dispatcher>REQUEST</dispatcher>  
            <dispatcher>FORWARD</dispatcher> 
    </filter-mapping>

    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener> <listener>
    <listener-class>
    org.springframework.web.context.request.RequestContextListener
    </listener-class>
    </listener> <servlet>
    <servlet-name>JspSupportServlet</servlet-name>
    <servlet-class>
    org.apache.struts2.views.JspSupportServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>
         
    </web-app>