我怎样才能得到HttpSession session = request.getSession(true);请求映射方法之外的值

时间:2017-07-05 07:35:07

标签: spring-mvc

这很好用

@RequestMapping(value = "/users/emailfrom2")
    public String displayLogin3(Model model, HttpServletRequest request, HttpServletResponse response) {

    HttpSession session = request.getSession(true);
    String gsSessionID = (String) session.getAttribute("GSSessionID");

    System.out.println("Session ===>>>> " + gsSessionID);
    logger.debug("displayLogin3()");

    return "users/test1";
}

但我想要这样。就像全球价值一样。

public class UserController  {
String gsSessionID = (String) session.getAttribute("GSSessionID");  
@RequestMapping(value = "/users/emailfrom2")
public String displayLogin3(Model model, HttpServletRequest request, HttpServletResponse response) {

            System.out.println("Session ===>>>> " + gsSessionID);
            logger.debug("displayLogin3()");
            return "users/test1";
        }

 @RequestMapping(value = "/users/emailfrom5")
    public String displayLogin5(Model model, HttpServletRequest request, HttpServletResponse response) {

     String sessionId = getSessionProperty(request, "GSSessionID");

        System.out.println("Session ===>>>> " + sessionId);
        logger.debug("displayLogin3()");

        return "users/test1";
    }

}

 @RequestMapping(value = "/users/emailfrom6")
public String displayLogin5(Model model, HttpServletRequest request, HttpServletResponse response) {

 String sessionId = getSessionProperty(request, "GSSessionID");

    System.out.println("Session ===>>>> " + sessionId);
    logger.debug("displayLogin3()");

    return "users/test1";
}

我只想写String sessionId = getSessionProperty(request,“GSSessionID”);控制器类中只有一次不是两次或三次或更多次。 任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

使用类似的东西:

public class UserController extends AbstractController {

   @RequestMapping(value = "/users/emailfrom2")
    public String displayLogin3(Model model, HttpServletRequest request, HttpServletResponse response) {

        String sessionId = getSessionID(request)

        System.out.println("Session ===>>>> " + sessionId);
        logger.debug("displayLogin3()");

        return "users/test1";
    }
}


public abstract class AbstractController {
    public String getSessionID(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        return (String) session.getAttribute("GSSessionID");
    }
}

<强>更新

如果你说你可以在会话中需要不同的值,你应该修改这个方法:

public abstract class AbstractController {

    protected String gSSessionID;
    protected String gSPresentDate;


    @PreProcess
    public void loadSessionAttributes(@SessionAttribute(name="GSSessionID") String gSSessionID,
                                      @SessionAttribute(name="GSPresentDate") String gSPresentDate){
        this.gSSessionID = gSSessionID;
        this.gSPresentDate = gSPresentDate;
        //Some other session attributes
    }
}

public class UserController extends AbstractController {

    @RequestMapping(value = "/users/emailfrom2")
    public String displayLogin3(Model model, HttpServletRequest request, HttpServletResponse response) {

        System.out.println("Session ===>>>> " + gSSessionID);
        logger.debug("displayLogin3()");

        return "users/test1";
    }
}