wood burning stoves 2.0*
The moose likes JSF and the fly likes Value inputtext can't bind to bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Value inputtext can Watch "Value inputtext can New topic
Author

Value inputtext can't bind to bean?

Jelle Oosterbosch
Greenhorn

Joined: Feb 28, 2011
Posts: 1
I have following bean:

public class LoginBean {
private String email;
private String password;
private SessionId sessionId;

private UserController userController;

public LoginBean() {
this.email = "";
this.password = "";
this.sessionId = SessionId.INVALID_SESSION_ID;
}

public void setUserController(UserController userController) {
this.userController = userController;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
System.out.println("New email:" + email);
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
System.out.println("New password:" + password);
this.password = password;
}


And have configured it in faces-config.xml

<faces-config>

<application>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>

<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>backingBean.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>userController</property-name>
<value>#{userController}</value>
</managed-property>
</managed-bean>


And also in the web.xml

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

And the jsp page is html and css and next part

<h:form id="form">
<p><h:inputText value="#{loginBean.email}" /></p>
<p><h:inputSecret value="#{loginBean.password}" /></p>
<p><h:message for="form"/></p>
<p><h:commandButton value="login" action="#{loginBean.login}" /></p>

</h:form>

I can't get the values out of the inputtext in any way? Does anyone see the mistake or something?
Matthew Cserhati
Greenhorn

Joined: Apr 04, 2014
Posts: 8
A couple of things:

you have to name your bean:

@Name("nameofbean")
@Scope(SESSION)

Make the name very similar to the name of the java file.

Also, in the faces-config file you might want to change this:

<value>#{userController}</value>

the variable value has to be: beanname.variablename

so make it: beanname.userController /first name your bean/
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

1. If you use the "Code" button in our message editor, it will create wrapper tags for your sample Java and XML that will make them easier to read.

2. It is incorrect to name a user-written class "xxxController" in JSF. All of the JSF MVC Controllers are pre-supplied in JSF. You only create View and Model objects.

3. Java has a pre-written, pre-debugged security (login system) that's part of the J2EE standard and has been for well over a decade. Please don't write your own login code except for study purposes. I've seen more java webapps than I can count that used user-written login code and not only were all of them insecure, most of them could be worked around by non-technical people in 10 minutes or less. The J2EE security system was designed by security professionals and I've never heard of it being hacked.

4. JSF is an Inversion-of-Control framework. As long as you have defined your backing bean's JSF context properly using annotations or entries in faces-config.xml, JSF will construct the bean as needed and use its internal controllers to automatically transfer data (bean properties) to and from the View. All your action methods have to do to get the form values is use the backing bean's "get" methods.

5. Your h:message tag is for your form, but I don't think forms ever produce messages. If you want messages related to specific controls, use the "for=" attribute to reference the id of the control you wish the message to be about. To get ALL messages displayed, use the "h:messages" tag. This is a good debugging trick, since JSF's contract says that if ANY control on the form has an invalid value, the JSF controller will not update the backing bean or invoke its action method.

6. Your example is JSF Version 1 code. JSF version 2 has been out a long time now. It uses xhtml instead of "JSP"s for its View Templates.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
GeeCON Prague 2014
 
subject: Value inputtext can't bind to bean?