簡體   English   中英

springMVC中的Httpsession管理

[英]Httpsession management in springMVC

我是初學MVC並開始通過做我學到的東西來做一個示例應用程序。 我打算在spring MVC中實現Session管理。 我發現這個有用。

但我無法清楚地理解它。 我們為會話添加值

HttpSession session = request.getSession(false);
session.setAttribute("key", value);
session.setAttribute("key1",  value1);

然后我們根據鍵獲取值

session.getAttrubute("key");

但是在春季的MVC中,我看不到任何類似的東西,這讓我感到很困惑。

@Controller
@SessionAttributes("thought")
public class SingleFieldController {

    @RequestMapping(value="/single-field")
    public ModelAndView singleFieldPage() {
        return new ModelAndView("single-field-page");
    }

    @RequestMapping(value="/remember")  
    public ModelAndView rememberThought(@RequestParam String thoughtParam) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("thought", thoughtParam);
        modelAndView.setViewName("single-field-page");
        return modelAndView;
    }

}

在上面的代碼中@SessionAttributes("thought")讓我感到困惑,就像這個thought定義的那樣,我也沒有必要返回一個ModelAndView因為我使用的是backbone.marionette.js

那么如何在會話中設置值並在需要時使用它們? 我還必須將會話對象轉換為我的用戶定義對象,因為在將值返回到屏幕時,我只返回會話中可用的UserDefined對象列表。

所以請幫助我更好地理解它。 也許我對使用jsp / servlet的方式感到困惑。

UPDATE

下面是我的控制器並提供了注釋

package com.hexgen.puppet;

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

import com.hexgen.puppet.CreatePuppet;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PuppetService {

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public @ResponseBody
    void createOrder(@RequestBody CreatePuppet request) {
        //logic to add/update values in session
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody
    List<Puppet> getGroups() {
        //logic to retrive objects from session and convert it as List and send it back

        return puppets;
    }


}

並在需要時轉換對象並繼續

doesn't fully replaces the traditional session management. 並沒有完全取代傳統的會話管理。 如果兩個或多個Controller方法需要傳達某些數據,請使用它。 但是,使用這個我們只能在單個控制器類中實現通信。 . 如果使用則不使用顯式讀取和寫入會話。 is suggested only for short lived communications. 建議僅對短期通信使用 and explicitly, instead of . 如果需要在會話中存儲長期數據,建議顯式使用 ,而不是 有關更多信息, 請查看此信息。

您可以像這樣在springmvc中處理會話管理。 這是一個控制器方法

@RequestMapping(value = { "/login" }, method = RequestMethod.POST)
@ResponseBody
public String login(HttpSession session,String username,String password) throws Exception {
    Member member=userService.authenticateUser(username, password);
    if(member!=null) {
        session.setAttribute("MEMBER", member);
    } else {
        throw new Exception("Invalid username or password");
    }
    return Utils.toJson("SUCCESS");
}

用戶將傳遞用戶名和密碼,而Spring將自動注入會話屬性。 我們將從db驗證此用戶名和密碼。 為此,我們將使用一些服務方法,該方法將調用某些存儲庫方法來獲取Member of Member類並在控制器中返回它。 在應用程序方法中,您需要保存在會話中的信息將其傳遞給處理程序的參數。 您可以在http://faisalbhagat.blogspot.com/2014/09/session-management-with-spring-mvc.html上的每個方法調用中找到有關如何驗證此信息的更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM