我想从jsp上的controller方法获取会话值。谁能帮助我如何做到这一点

时间:2016-01-24 14:40:34

标签: java session

我想从控制器方法获取会话值到jsp。有人可以帮助我如何做到这一点

controller.java

@RequestMapping(value="customerLogin.do", method=RequestMethod.POST)
public String customerLogin(HttpServletRequest request, HttpSession session,HttpServletResponse response) throws SQLException, IOException{
String customerEmail=request.getParameter("customerEmail");
String password= request.getParameter("customerPassword");
Boolean isLoggedIn=false;
Customer customer= serviceInter.logincustomer(customerEmail, password);
if(customer!=null){
    isLoggedIn=true;
}
response.getWriter().write(new Gson().toJson(isLoggedIn));
session.setAttribute("customerInfo", customer);
return null;
}

我需要在这里获得该值

  • Hello<客户名称>
  • 2 个答案:

    答案 0 :(得分:0)

    您可以使用以下声明来获取它:

    Customer customer = (Customer) session.getAttribute("customerInfo");
    //System.out.println(customer.getFirstName());
    

    答案 1 :(得分:-1)

    您如何在JSP中检索? 你得到的错误是什么?

    ${customerInfo.name} 
    OR 
    Customer customer = (Customer) session.getAttribute("customerInfo");
    

    第二个选项:您还可以使Controller成为会话范围。

    @Controller
    @Scope("session")
    public class SessionScopedController