Dmitri S.
Dmitri S.

Reputation: 3438

JSF inputText and Sessions in jsf managed bean

I would like to know how can i read a value of input text from the managed bean. I know it is possible to read this way and it is very stright forward.

<h:inputText id="username" value="#{mylogin.username}" required="true" />

But what if lets say i have a value like

 <h:inputText id="username" value="some_value" required="true" />

And i want to read this "some_value" in my managed bean. Is it possible ?

Another question is it possible to access to the session variables in managed bean or should i somehow pass them to there ?

Upvotes: 3

Views: 9186

Answers (3)

McDowell
McDowell

Reputation: 108889

<h:inputText id="username" value="#{mylogin.username}" required="true" />

This would be the best way, in my opinion. Is there a reason you would like to avoid this? You can, as harto suggests, use component binding, but (as Vinegar points out) you gain nothing from this approach.

<h:inputText id="username" value="some_value" required="true" />

If you want to read the above value, it is possible to read it directly from the request parameters. The ExternalContext encapsulates the underlying container API and can be accessed in a managed bean like so:

FacesContext facesContext = FacesContext
    .getCurrentInstance();
ExternalContext extContext = facesContext
    .getExternalContext();
Map<String, String> params = extContext
    .getRequestParameterMap();

But, aside from violating the model-view-presenter contract, you can run into some practical problems. The parameter key may not be "username", but might be something like "j_id_jsp_115874224_691:username", depending on whether you make the component a child of any NamingContainers (like UIForm - see the prependId attribute) or if the view is namespaced. Hard-coding this value anywhere is probably a bad idea. You can read about the relationship between JSF component IDs and rendered HTML IDs here. If you want to use UIComponent.getClientId to generate the the key, you are back to component binding because you need to get a reference to the component.

Another question is it possible to access to the session variables in managed bean..?

See ExternalContext.getSessionMap.

Upvotes: 3

harto
harto

Reputation: 90493

A common way to read a component value is to create a binding to the component in your managed-bean, and read the value from it.

For example:

<h:inputText id="username" value="some_value" required="true"
    binding="#{mylogin.usernameField}" />

Then, in the managed-bean:

private UIInput usernameField;

public void setUsernameField(UIInput usernameField) { 
    this.usernameField = usernameField;
}
public UIInput getUsernameField() { 
    return usernameField; 
}

Finally, to access the field value:

Object value = usernameField.getValue();

Upvotes: 2

Adeel Ansari
Adeel Ansari

Reputation: 39907

In the latter case that some_value is not bean managed, IMHO. Nonetheless, you can read that. Do something like this,

FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request =
      (HttpServletRequest)ctx.getExternalContext().getRequest(); 
request.getParameter("username");

Similarly, regarding accessing session variable,

FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request =
      (HttpServletRequest)ctx.getExternalContext().getSession(false); 

Upvotes: 2

Related Questions