有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (2) 个答案

  1. # 1 楼答案

    你有三个选择:

    1. 正确地使用SessionAware
    2. 正确操作,并使用使用SessionAware的基本操作类
    3. 做错事并使用ActionContext
    Map attibutes = ActionContext.getContext().getSession();
    

    记录在Struts 2 wiki的How do we get access to the session

    为什么你想使用SessionAware,让你的行为更容易测试

  2. # 2 楼答案

    如果不想在多个类中使用SessionAware,那么至少可以使用一个抽象类或动作类扩展的接口。它将向动作类实例注入SessionMap

    获得SessionMap或直接HttpSession的其他方法是从here获得:

    If you want to put something to the session you should get the session map from the action context

    Map<String, Object> session = >ActionContext.getContext().getSession();
    session.put("username", username);
    session.put("role", 1);
    

    or use servlet session directly

    HttpSession session = >ServletActionContext.getRequest().getSession(); 
    session.setAttribute("username", username);
    session.setAttribute("role", 1);
    

    But the first case is preferable, due to it's supported by the framework.


    更多其他选项(您至少还有五个选项):