Wang.J Wang.J - 24 days ago 6
Java Question

How can i get session from spring web flow xml?


i read spring in action 4,but i got confused that the example did't show how to get Session from request.
i want to achieve a complete login flow,i want to get the session from jsp to check if the user exist.
The example just showed requestParameters this class?(i even don't know what it is)
And by using point '.' to get the parameter from Get method.But how can i get a session?
When if the method is Post?

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

Answer

I think you are talking about spring MVC and Spring is used for dependency injection of the classes which you need in your application.

i) To keep the user object in session, have HttpSession object in parameter for the controller class which should be session scoped and store the session value in user object.

For example:

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

ii) Also make the User Object class as session object by scope

For example:

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

iii) You can have XML file for further dependency like AOP etc.

For example:

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