spring 如何在 mvc 控制器中使用 @Scope("session") 保存的 jsp 页面中会话变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12149820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:20:17  来源:igfitidea点击:

How to use the values from session variables in jsp pages that got saved using @Scope("session") in the mvc controllers

springspring-mvc

提问by droidsites

Doing a web site using spring mvc. I added a SignupController to handle all the sign up related requests. Once user signup I am adding that to a session using @Scope("session"). Below is the SignupController code,

使用 spring mvc 做一个网站。我添加了一个 SignupController 来处理所有与注册相关的请求。用户注册后,我将使用@Scope("session") 将其添加到会话中。下面是 SignupController 代码,

SignupController.java

注册控制器.java

@Controller
@Scope("session")
public class SignupController {

@Autowired
SignupServiceInter signUpService;

private static final Logger logger = Logger.getLogger(SignupController.class);

private String sessionUser;

@RequestMapping("/SignupService")
public ModelAndView signUp(@RequestParam("userid") String userId, 
    @RequestParam("password") String password,@RequestParam("mailid") String emailId){

    logger.debug("  userId:"+userId+"::Password::"+password+"::");

    String signupResult;

    try {

        signUpService.registerUser(userId, password,emailId);
        sessionUser  = userId; //adding the sign up user to the session
        return new ModelAndView("userHomePage","loginResult","Success"); 
         //Navigate to user Home page if everything goes right

    } catch (UserExistsException e) {
        signupResult = e.toString();
        return new ModelAndView("signUp","loginResult", signupResult); 
         //Navigate to signUp page back if user does not exist
    }
}
 }

I am using "sessionUser" variable to store the signed up User Id. My understanding is that when I use @Scope("session") for the controller all the instance variables will added to HttpSession. So by that understanding I tried to access this "SessionUser" in userHomePage.jsp as,

我正在使用“sessionUser”变量来存储已注册的用户 ID。我的理解是,当我对控制器使用 @Scope("session") 时,所有实例变量都将添加到 HttpSession。因此,根据这种理解,我尝试在 userHomePage.jsp 中访问这个“SessionUser”,

userHomepage.jsp

用户主页.jsp

Welcome to <%=session.getAttribute("sessionUser")%>

But it throws null.

但它抛出空值。

So my question is how to use the values from session variables in jsp pages that got saved using @Scope("session") in the mvc controllers.

所以我的问题是如何使用 jsp 页面中会话变量的值,这些值是在 mvc 控制器中使用 @Scope("session") 保存的。

Note: My work around is that pass that signed User Id to jsp page through ModelAndView, but it seems passing the value like these among the pages takes me back to managing state among pages using QueryStrings days.

注意:我的解决方法是通过 ModelAndView 将签名的用户 ID 传递到 jsp 页面,但似乎在页面之间传递这些值使我回到使用 QueryStrings 天管理页面之间的状态。

采纳答案by Biju Kunjummen

When you specify @Scope("session") for the controller, Spring will now ensure that there is a unique instance of the controller for every session, and so you will be able to keep a session specific state like sessionUser as a instance variable.

当您为控制器指定 @Scope("session") 时,Spring 现在将确保每个会话都有一个唯一的控制器实例,因此您将能够将会话特定状态(如 sessionUser)保持为实例变量。

However to expose a variable to the UI, that variable has to be a part of the model/session attributes, you can do that either with @ModelAttribute("sessionUser") or @SessionAttribute("sessionUser")

但是,要向 UI 公开变量,该变量必须是模型/会话属性的一部分,您可以使用 @ModelAttribute("sessionUser") 或 @SessionAttribute("sessionUser")

@Controller
@SessionAttribute("sessionUser")
public class SignupController {
@RequestMapping("/SignupService")
public ModelAndView signUp(@RequestParam("userid") String userId, 
    @RequestParam("password") String password,@RequestParam("mailid") String emailId,  Model model){
...
    try {

        signUpService.registerUser(userId, password,emailId);
        sessionUser  = userId; //adding the sign up user to the session
        model.addAttribute("sessionUser", sessionUser);

        return new ModelAndView("userHomePage","loginResult","Success"); 
         //Navigate to user Home page if everything goes right
       ....
}
}

回答by Peter Butkovic

I've never tried spring integration with jsp, as I've been always using jsf, but I would give this a try:

我从未尝试过 Spring 与 jsp 集成,因为我一直在使用 jsf,但我会尝试一下:

First: add getter and setter for the property you're interested in (to the controller).

首先:为您感兴趣的属性添加 getter 和 setter(到控制器)。

@Controller
@Scope("session")
public class SignupController {

...

private String sessionUser;

public void setSessionUser(String sessionUser) {
    this.sessionUser = sessionUser;
}

public String getSessionUser() {
    return this.sessionUser;
}

...

then: call it in jsp like this:

然后:像这样在jsp中调用它:

Welcome to ${session.sessionUser}

does that work for you? :) If not, I'd try with:

那对你有用吗?:) 如果没有,我会尝试:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
Welcome to <c:out value="${session.sessionUser}" />