tags:

views:

2666

answers:

3

Hello everybody. First of all i would like to say that i got so much help in java in last few days, and would like to thanks everybody for their help.

Now to my question. I am very new to JSF...

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 ?

Some basics code examples will be really great.

Thanks in advance.

+1  A: 

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);
Adeel Ansari
The parameter name may not be "username" (see my answer); also, you can avoid casting to the servlet API if you want to.
McDowell
I really like this way since it not ties bean to JSF.
Dmitris
+2  A: 

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();
harto
That would be component binding. Not a good idea, IMHO. Thats the reason I didn't mention it. But yes its the way. Actually in this case you lose the simplicity of POJO, and tie your bean completely with JSF.
Adeel Ansari
Sure - it's not practical unless you wanted to do something else with the component. The best way to do it is well-known - just use a value binding expression.
harto
+3  A: 
<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.

McDowell
Thank you very much for taking time and explaining things to me. I really appreciate it.The reason i asked is because JSF is first java framework i am studying and I really like it. The question was for education purpose(prior to it i tryed to goodle, but didnot find no good explanation), in reall application i am not going use it like that, but i wanted to know how it can be done.
Dmitris
Ah, I quite forgot about the naming issue. Thanks for pointing out. Futher dealing with Maps is a better idea. +1
Adeel Ansari