设置会话变量spring mvc 3 [英] Set session variable spring mvc 3

查看:111
本文介绍了设置会话变量spring mvc 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 $ {variable} $ {requestScope}设置我可以在任何视图中使用的会话对象。变量}

为了能够使用会话,我需要设置<%@ page session =true %>

To be able to use sessions do I need to set <%@ page session="true" %> ?

推荐答案

如果您想在视图中访问会话变量最简单这样做的方法是:

If you want to access a session variable in your view the easiest way to do it is :

${sessionScope.yourVariable} 

请参阅使用范围对象了解更多信息。

如果您设置<%@ page session =true> 然后JSP将会话范围和页面范围合并到一个命名空间中。然后你可以这样做:

If you set <%@ page session="true"> then the JSP will merge the session scope and at the page scope into a single namespace. Then you can do:

${yourVariable}

你可以在这样的mvc控制器中放入一些东西:

You can put something into the session in a mvc controller like this:

@RequestMapping("/test")
@Controller
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public String testMestod(HttpServletRequest request)
    {
        request.getSession().setAttribute("testVariable", "Test Values!!");
        return "testJsp";
    }
}

最后,@ SessionAttribute用于特定用途case,并没有将变量放入会话,以便任何人都可以访问它们:

Finally, the @SessionAttribute is meant for a specifc use case, and doesn't put variables into the session so that anyone can access them:

以下是春天人们如何描述@SessionAttribute的功能:

Here is how the spring folks describe the functionality of @SessionAttribute:


@SessionAttributes在
中的工作方式与
SimpleFormController的sessionForm相同。它将
命令(或@SessionAttributes
任何对象)放在第一个和
最后一个请求之间的
持续时间内(大部分时间是$ b) $ b初始GET和最后的POST)。在
之后删除了这些东西。

The @SessionAttributes works in the same way as the sessionForm of the SimpleFormController. It puts the command (or for the @SessionAttributes any object) in the session for the duration between the first and the last request (most of the time the initial GET and the final POST). After that the stuff is removed.

每个Controller都有自己的ModelMap
所以在controller1 $中放入
@SessionAttributes b $ b在控制器2中不可用,而
则相反。为了工作你将
必须自己手动把东西放在会议上

Each Controller has it's own ModelMap so something put as a @SessionAttributes in controller1 isn't available in controller2 and vice versa. For that to work you will have to put stuff on the session manually yourself.

这篇关于设置会话变量spring mvc 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆