如何在 Spring-mvc 中使用 Session 属性 [英] How to use Session attributes in Spring-mvc

查看:79
本文介绍了如何在 Spring-mvc 中使用 Session 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我写一下这段代码的spring mvc风格的模拟吗?

Could you help me write spring mvc style analog of this code?

 session.setAttribute("name","value");

以及如何将通过 @ModelAttribute 注释添加到会话中的元素然后访问它?

And how to add an element that is annotated by @ModelAttribute annotation to session and then get access to it?

推荐答案

如果你想在每次响应后删除不需要会话的对象,

If you want to delete object after each response you don't need session,

如果您想在用户会话期间保留对象,有一些方法:

If you want keep object during user session , There are some ways:

  1. 直接给 session 添加一个属性:

  1. directly add one attribute to session:

@RequestMapping(method = RequestMethod.GET)
public String testMestod(HttpServletRequest request){
   ShoppingCart cart = (ShoppingCart)request.getSession().setAttribute("cart",value);
   return "testJsp";
}

你可以像这样从控制器获取它:

and you can get it from controller like this :

ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");

  • 将您的控制器会话设置为范围

  • Make your controller session scoped

    @Controller
    @Scope("session")
    

  • 作用域对象,例如您有每次都应该在会话中的用户对象:

  • Scope the Objects ,for example you have user object that should be in session every time:

    @Component
    @Scope("session")
    public class User
     {
        String user;
        /*  setter getter*/
      }
    

    然后在你想要的每个控制器中注入类

    then inject class in each controller that you want

       @Autowired
       private User user
    

    让课程继续上课.

    AOP 代理注入:在 spring-xml 中:

    The AOP proxy injection : in spring -xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
      <bean id="user"    class="com.User" scope="session">     
          <aop:scoped-proxy/>
      </bean>
    </beans>
    

    然后在你想要的每个控制器中注入类

    then inject class in each controller that you want

    @Autowired
    private User user
    

  • 5.将HttpSession传递给方法:

    5.Pass HttpSession to method:

     String index(HttpSession session) {
                session.setAttribute("mySessionAttribute", "someValue");
                return "index";
            }
    

    6.Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):

    6.Make ModelAttribute in session By @SessionAttributes("ShoppingCart"):

      public String index (@ModelAttribute("ShoppingCart") ShoppingCart shoppingCart, SessionStatus sessionStatus) {
    //Spring V4
    //you can modify session status  by sessionStatus.setComplete();
    }
    

    或者您可以将模型添加到整个控制器类,例如,

    or you can add Model To entire Controller Class like,

    @Controller
        @SessionAttributes("ShoppingCart")
        @RequestMapping("/req")
        public class MYController {
    
            @ModelAttribute("ShoppingCart")
            public Visitor getShopCart (....) {
                return new ShoppingCart(....); //get From DB Or Session
            }  
          }
    

    各有优缺点:

    @session 可能会在云系统中使用更多的内存,它会将 session 复制到所有节点,而直接方法(1 和 5)的方法比较混乱,不利于单元测试.

    @session may use more memory in cloud systems it copies session to all nodes, and direct method (1 and 5) has messy approach, it is not good to unit test.

    访问会话jsp

    <%=session.getAttribute("ShoppingCart.prop")%>
    

    在 Jstl 中:

    <c:out value="${sessionScope.ShoppingCart.prop}"/>
    

    在百里香中:

    <p th:text="${session.ShoppingCart.prop}" th:unless="${session == null}"> . </p>
    

    这篇关于如何在 Spring-mvc 中使用 Session 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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