我的问题是我想将值(登录用户的ID)传递给页面。 以下代码显示了页面的链接
<h:form>
<h:link outcome="Account">
#{logging.username}
</h:link>
</h:form>
所以我想将“logging.id”传递给“帐户”,当该页面加载时我想将id传递给该Account页面使用的另一个支持bean。 那么这样做的方法是什么? 请帮助我
答案 0 :(得分:1)
就我而言,登录用户是会话属性而不是请求/页面属性。
就此而言,您可以将user.id保存在session属性中,而不是通过url请求将其传递到另一个页面,而是从会话中从任何支持bean获取它。/ p>
示例:
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");
}
}