简体   繁体   English

JSF 1.2中的会话处理

[英]session handling in JSF 1.2

How do we handle session variables (add/fetch) in JSF 1.2? 我们如何处理JSF 1.2中的会话变量(添加/获取)?

A scenario: Consider a login screen where the user logs in successfully, the user model is stored in session. 场景:考虑用户成功登录的登录屏幕,用户模型存储在会话中。 The user modes contains the user role. 用户模式包含用户角色。 Next time onwards, for every user action, check the user role from the user model and display the form accordingly. 下次,对于每个用户操作,检查用户模型中的用户角色并相应地显示表单。 In such a case how to add the user odel in session and how to etrieve it every time from the session? 在这种情况下,如何在会话中添加用户odel以及如何在每次会话中执行它?

Previously I have worked in Struts 1.2 where in the execute method, we have a request e=which is used to get the session and access the session variables also. 以前我在Struts 1.2中工作过,在execute方法中,我们有一个request e =,用于获取会话并访问会话变量。 But I am not sure how to achieve th same in JSF 1.2. 但我不确定如何在JSF 1.2中实现相同的功能。

Is the only way achieveable is to add the managed bean in the session scope in the faces-config.xml file? 唯一可行的方法是在faces-config.xml文件中的会话范围中添加托管bean吗?

Please help me out with the session handling concepts in JSF 1.2. 请帮我解决JSF 1.2中的会话处理概念。

The session scope is programmatically available by ExternalContext#getSessionMap() which delegates under the covers to HttpSession#get/setAttribute() . 会话范围由ExternalContext#getSessionMap()以编程方式提供,它将ExternalContext#getSessionMap()委托给HttpSession#get/setAttribute()

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
// ...

You can of course also just put a managed bean in the session scope. 您当然也可以将托管bean放在会话范围内。 It's accessible from other managed beans by <managed-property> (or just a traversal of the session map using the managed bean name as map key). 可以通过<managed-property>从其他托管bean访问它(或者只使用托管bean名称作为map键遍历会话映射)。

I think you can use Java EE filters for this mechanism. 我认为您可以使用Java EE过滤器来实现此机制。

Filters are controlled by Servlet Container and runs first on an action depending on your web.xml order. 过滤器由Servlet Container控制,并根据your web.xml顺序首先在操作上运行。

Add a servlet filter to your project. 将servlet过滤器添加到项目中。

public class YourFilter implements Filter {

public static final String USER = "USER_SESSION_KEY";

public void doFilter(ServletRequest req, ServletResponse response, FilterChain filterChain)
{

   HttpServletRequest request = (HttpServletRequest) req;
   HttpSession session = request.getSession(true);
       String servletpath = request.getServletPath();
if(!servletpath.contains("login.xhtml")) //exclude your login page and other pages required to pass this filter.
{
     if (session.getAttribute(USER) != null)
    {
    //Control your authentication and roles.
    }
    else
    {
    //There is no user in the session.
    }
}


    }
    filterChain.doFilter(request, response);
    }

Add your filter to your web.xml 将过滤器添加到web.xml

<filter>
    <filter-name>YourFilter</filter-name>
    <filter-class>Package.YourFilter</filter-class>
</filter>
  <filter-mapping>
    <filter-name>YourFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>

Secondly, put your User class to the session inside a JSF action. 其次,将您的User类放入JSF操作中的会话中。

public void userAction()
{


 User user = new User();
   //Build your User Class
  HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    request.getSession(false).setAttribute("USER", user);

}

PS : User class is a user defined POJO class. PS:用户类是用户定义的POJO类。 you should implement it according to your needs. 你应该根据你的需要实现它。

public class User
{
    private String username;
       //Other properties and getter setter methods required.

}

If you want to implement this mechanism inside JSF context. 如果要在JSF上下文中实现此机制。 You can build the same logic by implementing JSF phase listeners . 您可以通过实现JSF阶段侦听器来构建相同的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM