2016-09-19 14 views
0


我在閱讀spring 4的動作,但是我很困惑這個例子沒有說明如何從請求中獲取Session。
我想實現一個完整的登錄流程,我想從jsp獲取會話來檢查用戶是否存在。
這個例子剛剛展示了requestParameters這個類?(我甚至不知道它是什麼)
並通過使用'。'從Get方法獲取參數。但我如何獲得會話?
如果方法是Post?如何從彈簧web流xml中獲取會話?

<action-state id="lookupCustomer"> 
    <evaluate result="order.customer" 
     result-type="com.springinaction.pizza.domain.Customer" 
     expression="pizzaFlowActions.lookupCustomer(requestParameters.phoneNumber)" /> 
    <transition to="registrationForm" 
     on-exception="com.springinaction.pizza.service.CustomerNotFoundException" /> 
    <transition to="showOrder" /> 
</action-state> 
+0

總之,我只想從流程中獲取會話,登錄的人,用戶信息將保存在會話中,我只是想獲得它,以便我可以繼續下一個流程。 –

回答

1

我想你是在談論春天MVC和Spring是用於依賴注入你在你的應用程序中需要的類。

i)爲了保持用戶對象在會話中,在控制器類的參數中有HttpSession對象,該控制器類應該是會話作用域並將會話值存儲在用戶對象中。

例如:

 @Scope("session") 
    @Controller 
    public class UserController { 
    @RequestMapping(method = RequestMethod.GET) 
    public String testMestod(HttpServletRequest request){ 
    User user=(User)session.getAttribute("cart"); 
    return "testJsp"; 
    } 
    } 

ⅱ)也使用戶對象類作爲會話對象由範圍

例如:

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

ⅲ)可以具有用於進一步的XML文件依賴如AOP等

例如:

<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> 
+0

真的非常感謝您的評論,util現在我仍然從HttpServletRequet獲得會話,但實際上我在談論Spring Web Flow這個框架,我真的很難過,我不知道如何在配置文件中獲取會話。我需要用戶的用戶名。 –

+0

此鏈接可能會幫助你: http://stackoverflow.com/questions/8913062/spring-webflow-how-to-pass-the-session-in-evaluate-expression –

+0

上帝!非常感謝! –