webwork做的网站,现在需要无论从网站任何一个页面进入网站,都能先从后台查询变量x然后设置变量x到session里,然后再在这个页面里把变量x取出来。

解决方案 »

  1.   

    先在我的网站数据库中有个库存变量storage,int类型。设计网站时只是在打开首页index.jsp的时候先执行了后台index.action在里面查询了storage,然后设置到session中,在index.jsp页面中取到storage。但是如果首先打开其他页面由于没有先执行后台index.action,所以在这个页面取不到storage。我想在过滤器里打开每个页面时执行查询操作设置storage到session中,但是由于webwork中HttpSession httpSession = ServletActionContext.getRequest().getSession();但是由于不是在webwork的action中,所以ServletActionContext.getRequest()取到的request为空会报异常,所以该咋办?
      

  2.   

    先在页面里读取session中的x,如果有值,则没问题,否则让他跳转到一个固定页面,类似登录,在这个页面里从后台查询变量x就行了
      

  3.   

    你建个拦截器啊,在拦截器里面查询出这个变量,然后放到session。。
      

  4.   

    过滤器里可以。
     public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain filterChain)
            throws IOException, ServletException
        {
            try
            {
                filterChain.doFilter(request, response);
                
            }
            finally
            {
                try
                {
                  //查询库存变量
                    HttpServletRequest req= (HttpServletRequest)request;
                    HttpSession httpsession=req.getSession();
                    Object ob=httpsession.getAttribute("storage");
                    if(ob==null)
                    {
                     Session s= HibernateUtil.currentSession();
                     Storage st = (Storage)s.load(Storage.class, new Long(1));
             storage = st.getStorage();
             httpsession.setAttribute("storage", storage);
                    }
                    else
                    {
                     storage = ob.toString();
                    }
                    //查询库存变量
                    System.out.println("****************"+storage);
                    HibernateUtil.closeSession();              
                   
                }
                catch (Exception e)
                {
                    System.out.println(e);
                }
            }
        }