我的问题是我想将一个值(已登录用户的ID)传递给页面。以下代码显示了该页面的链接
<h:form>
<h:link outcome="Account">
#{logging.username}
</h:link>
</h:form>
所以我想在该页面加载时将“ logging.id”传递给“ Account”,我想将ID传递给该Account页面使用的另一个支持bean。那么这样做的方法是什么呢?请帮助我
就我而言,已登录的用户是会话属性,而不是请求/页面属性。
为此,您可以将user.id保存在session属性中,而不是通过url请求将其传递到另一个页面,而是从需要它的任何支持bean的会话中获取它。
例子:
public void login(String username, String password ){
UserAccount account = myManagerBean.findUser(username, password);
HttpSession session = getCurrentRequestFromFacesContext().getSession(false);
session.addAttribute("user.account", account);
}
然后从任何其他后备bean。
public UserAccount getUserAccount() {
HttpSession session = getCurrentRequestFromFacesContext().getSession(false);
return session.getAttribute("user.account");
}
但是,如果这就是您现在所需要的,则可以将id作为请求参数传递:
<h:form>
<h:link outcome="Account">
<f:param name="user.username" value="#{logging.username}"/>
</h:link>
</h:form>
然后,您可以将其直接附加到请求作用域bean的backing bean属性,或从请求中手动检索它:
@RequestScope
public class MyAccountBean {
@ManagedProperty("user.username")
private String username;
... or ...
public String getUserName() {
return getCurrentRequestFromFacesContext().getParameter("user.username");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句