简体   繁体   中英

How to pass JSF value to from a backing bean to another backing bean?

my problem is that i want to pass a value (id of the logged in user) to a page. the following code shows the link to the page

<h:form>
   <h:link outcome="Account">
       #{logging.username}
   </h:link>
</h:form>

so i want to pass the "logging.id to the "Account" when that page loads i want to pass the id to another backing bean used by that Account page. so what is the way to do this ? please kindly help me

As far as i am concerned, the logged in user is a session property rather than a request/page property.

For that matter, you could save the user.id in the session attribute, and rather than passing it to another page through a url request, you get it from the session from any backing bean you so require it from.

example:

public void login(String username, String password ){
  UserAccount account = myManagerBean.findUser(username, password);
  HttpSession session = getCurrentRequestFromFacesContext().getSession(false);
  session.addAttribute("user.account", account);
}

And then from any other backing bean.

public UserAccount getUserAccount() {
  HttpSession session = getCurrentRequestFromFacesContext().getSession(false);
  return session.getAttribute("user.account");
}

But if thats now what you require, you could pass the id as a request parameter:

<h:form>
   <h:link outcome="Account">
       <f:param name="user.username" value="#{logging.username}"/>
   </h:link>
</h:form>

You could then attach this directly to a backing bean property of a request scope bean or manually retrieve it from the request:

@RequestScope

public class MyAccountBean {

 @ManagedProperty("user.username")
 private String username;

 ... or ...
 public String getUserName() {
   return getCurrentRequestFromFacesContext().getParameter("user.username");
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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