简体   繁体   中英

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

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 ".
  2. Try debugging in Eclipse and stop at this line: 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
  3. Add a log or sysout statement in the controller to display attributes that you had set.
  4. Are you using Spring security? Is there any function that invalidates the session or calls 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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