求教:session.getAttribute()获取不到session.setAttribute()的值

很简单的一个web项目中,用户登陆成功后,在后台用session.setAttribute(“user”),记录登陆的用户信息,在跳到主页面(index.jsp)的时候,先经过后台处理,通过登陆的用户信息,查询相应的权限资源,这时用session.getAttribute(“user”),取到用户的信息,现在就是取不到用户信息,取到的值为null,经过不断测试,原因应该在后台登陆方法中的session.setAttribute(“user”),在其他方法里面都可以使用,但是登陆方法里面的session发送的任何信息,其他地方都接收不到,只有在本方法里面可以接收到,代码如下:

package com.hs.Iservice;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.hs.dao.IGroupDAO;
import com.hs.dao.IPermissionDAO;
import com.hs.dao.IUserDAO;
import com.hs.model.Group;
import com.hs.model.Permission;
import com.hs.model.User;
import com.hs.service.IUserService;

@Service
public class UserService implements IUserService {

    @Resource
    private IUserDAO dao;
    @Resource
    private IGroupDAO gdao;
    @Resource
    private IPermissionDAO pdao;

    //登陆 判断用户名、密码是否正确
    @Override
    public void login(User user, Boolean ptys , Boolean zgys) throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpSession session = request.getSession();
        String urlName = request.getRequestURI();
        System.out.println("request地址:"+urlName);
        session.setAttribute("abc", "abc");
        PrintWriter out = response.getWriter();
        User u = dao.login(user.getLoginId(),user.getPassword());
        //判断登陆账号、密码是否存在
        if (null != u) {
            // 普通医生登陆判断
            if (ptys == true && u.getGid() == 2) {
                session.setAttribute("user", u);
                out.write("{\"msg\":\"ok\"}");
                // 主管医生登陆判断
            } else if (zgys == true && u.getGid() == 1) {
                session.setAttribute("user", u);
                out.write("{\"msg\":\"ok\"}");
                // 管理员登陆判断
            } else if (ptys == false && u.getGid() == 0) {
                session.setAttribute("user", u);
                out.write("{\"msg\":\"ok\"}");
            } else {
                out.write("{\"msg\":\"gid\"}");
            }
        } else {
            out.write("{\"msg\":\"no\"}");
        }
    }
    //登陆成功后 跳转主页面 根据登陆的用户 查询相应的权限并发送至前台
    @Override
    public void index() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("b", "b");
        String urlName = request.getRequestURI();
        System.out.println("request时间:"+urlName);
        /*User u = (User) session.getAttribute("user");
        System.out.println("asdadasdad:"+u.getName());*/
        String a = (String) session.getAttribute("abc");
        System.out.println("dadadad:"+a);
        User use = dao.findById(4);
        List<Group> group = gdao.findByGroupId(use.getGid());
        List<Permission> list = new ArrayList<Permission>();
        String[] strs = group.get(0).getModuleId().split(",");
        for (String str : strs) {
            list.add(pdao.findById(Integer.parseInt(str)));
        }
        //处理list集合里面的数据按照指定顺序排序
        Collections.sort(list, new Comparator<Permission>(){
            @Override
            public int compare(Permission arg0, Permission arg1) {
                int i = arg0.getNumber()-arg1.getNumber();
                return i;
            }
        });
        request.setAttribute("list", list);
    }

}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页