简体   繁体   English

会话只能在Spring MVC中使用一次,

[英]Session only works one time spring mvc,

@Controller
public class LoginController {

     @RequestMapping(value = "showLogin", method = RequestMethod.GET)
     public ModelAndView showLogin(HttpServletRequest request, HttpSession session) {

     return new ModelAndView("login");
     }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpSession session) {

        String page = "login";
        String loginId = request.getParameter("loginId");
        String password = request.getParameter("password");
    //  HttpSession session = request.getSession();
        session.setAttribute("USERNAME", loginId);

        if(loginId.equals("admin") && password.equals("admin")){
            session.setAttribute("loginId", "admin");
            session.setAttribute("userName", "admin");
            session.setAttribute("userDetails", "System Administrator");
            session.setAttribute("USERNAME", "admin");

            page = "home";
            return page;
        }
@RequestMapping(value = "projdet", method = RequestMethod.GET)
    public String project(
            @ModelAttribute("projMaster") ProjectMaster projMaster,
            Model model, HttpServletRequest request, HttpSession session) {

        List<ProjectMaster> allProjectlist =getMasterDaoService().getAllProjList();
    model.addAttribute("Projlist", allProjectlist);

        String page = "project";
        return page;
    }

Session only works one time spring mvc,once login returns home page but from home to next screen no user details and session not found in any other controller 会话只能在Spring MVC中运行一次,一旦登录返回首页,但从首页到下一个屏幕,则在其他任何控制器中均未找到用户详细信息和会话

I have a few suggestions: 我有一些建议:

  1. This is probably not the root cause of the issue but your request mapping URL for the third controller function says " projdet ". 这可能不是问题的根本原因,但是您的第三个控制器功能的请求映射URL表示“ projdet ”。
  2. Try debugging in Eclipse and stop at this line: List<ProjectMaster> allProjectlist =getMasterDaoService().getAllProjList(); model.addAttribute("Projlist", allProjectlist); 尝试在Eclipse中调试并在此行停止: List<ProjectMaster> allProjectlist =getMasterDaoService().getAllProjList(); model.addAttribute("Projlist", allProjectlist); List<ProjectMaster> allProjectlist =getMasterDaoService().getAllProjList(); model.addAttribute("Projlist", allProjectlist); . Check the session variable thoroughly to see if it has the attributes you set on login 彻底检查session变量以查看其是否具有您在登录时设置的属性
  3. Add a log or sysout statement in the controller to display attributes that you had set. 在控制器中添加log或sysout语句以显示您已设置的属性。
  4. Are you using Spring security? 您正在使用Spring安全性吗? Is there any function that invalidates the session or calls logout ? 是否有任何使会话无效或调用logout

While these may not point out the exact solution, it may help you figure out the issue. 尽管这些可能无法指出确切的解决方案,但可以帮助您找出问题所在。 From what you have presented in the question, there is no evidence of anything wrong in the code. 根据您在问题中提出的内容,没有证据表明代码中有任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM