0
votes

So I'm having a problem trying to pass a String value.

The String value is entered through a login page as username.

The JSF then calls the Bean to verify log in information then proceeds to another JSF page.

I was wondering how to pass the username along to the new JSF page. Thank you.

2
What have you already tried? What are the errors you are getting? More people will be able to help you if you show what steps you have already taken. Please see how to ask a good question. - Nick

2 Answers

6
votes

If you're performing a navigation instead of a redirect, then you basically don't need to do anything. The information is also just available in the navigated page.

E.g. in login page,

<h:inputText value="#{bean.username}" />

and in the navigated page:

<p>You have entered the following username: #{bean.username}</p>

If you're however performing a redirect instead of a navigation, then you basically need to store the information in a bit broader scope. You didn't clearly elaborate the concrete functional requirement in the question, but if I guess it right, you just wanted to remember the currently logged-in user for the remaining of the HTTP session. In that case, just store it in the session scope during the login action.

public String login() {
    // ...
    User user = userService.find(username, password);
    // ...
    externalContext.getSessionMap().put("user", user);
    // ...
    return "nextpage?faces-redirect=true";
}

This way it's available by #{user} throughout the entire HTTP session.

<p>You're logged in as #{user.name}.</p>
2
votes

You can also use <t:saveState> without using session scope. <t:saveState> is longer than the request scope but shorter than session scope.

This may help you : http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_saveState.html