如何在控制器之间传递@SessionAttributes值

时间:2019-02-13 11:28:56

标签: spring-boot session

  

我的第一个控制器是Login


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;


import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@SessionAttributes("session")
public class LoginController extends GlobalController {

      @RequestMapping(value = "/", method = RequestMethod.GET)
        public String indexAction() throws IOException {
            return "login";
        }

        @RequestMapping(value = "/", method= RequestMethod.POST)
        public String indexAction(@RequestParam String username, @RequestParam String password,HttpSession session) {
          String page = "login";
           if(username != "" && password != ""){
              try {
                  if(userService.authenticationUser(username,password) == "success"){
                      page = "redirect:/main";
                      session.setAttribute("test","Salom");

                      //this.httpSession =session;
                      //System.out.println(session.getAttribute("test"));
                  }
                  else page = "login";
               }
              catch (Exception e){
                  e.fillInStackTrace();
              }
           }
          else page = "login";
          return page;
        }



    }
  

我的第二个控制器是测试


package com.springboot.app.controllers.reports;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

@SessionAttributes("session")
public class TestController {

       @RequestMapping(value = "/test",method = RequestMethod.GET)
       public String index(){
           @SessionAttributes("session")HttpSession session;
           return "";
       }



} 

--------------------------------------------------- -----------------------------如何从登录控制器向测试控制器传递@SessionAttributes(“ session”)或如何存储@变量中的SessionAttributes(“ session”)

2 个答案:

答案 0 :(得分:0)

@SessionAttributes不能用于(也不能用于)在不同控制器之间的会话中存储对象。带有@SessionAttributes注释的控制器还必须告知它已完成(因此controllerA而不是ControllerB)。来自控制器a的模型消息仍然不适用于控制器B。

请参阅this conversation

答案 1 :(得分:0)

您可以将LoginController传递给TestController:

e.g. Username = name

LoginController:

import org.springframework.ui.ModelMap;

@Controller
@SessionAttributes("name")
public class LoginController{

    @RequestMapping(value="/login", method = RequestMethod.POST)
    public String showWelcomePage(ModelMap model, @RequestParam String name, @RequestParam String password){

        boolean isValidUser = service.validateUser(name, password);

        if (!isValidUser) {
            model.put("errorMessage", "Invalid Credentials");
            return "login";
        }

        model.put("name", name);
        model.put("password", password);

        return "welcome";


TestController:

@SessionAttributes("name")

public void showUsername(ModelMap model){
    System.out.println("Username is: " + (String) model.get("name");
}


}

hope there is no typo!
相关问题