Wednesday, May 23, 2012

JSF page does not properly set object attributes

Simple question from a beginner at JSF:



I have very simple JSF form:



<h:form>
<p>#{messages.loginTextfieldUsername}</p>
<h:inputText value="#{userServiceImpl.user.name}" />

<p>#{messages.loginTextfieldPassword}</p>
<h:inputSecret value="#{userServiceImpl.user.password}" />

<h:commandButton value="#{messages.loginButtonLogin}" action="#{userServiceImpl.authenticateUser}" />
</h:form>


The userServiceImpl class is:



@Named
@RequestScoped
public class UserServiceImpl implements UserService {

private UserSession userSession;
private User user;

@Inject
public UserServiceImpl(UserSession userSession) {
this.userSession = userSession;
}

@PostConstruct
public void prepareService() {
user = new User();
}

@Override
public View authenticateUser() {
userSession.setLoggedUser(user);
return View.MAIN;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}


}



My goal is pretty simple: when the user hits the login button, I want to authenticate the user.



The problem is:
When the authenticate method is called, the User attributes are null. I debugged the application and the getUser method is called and the values are properly set, but at some point (which I did not find [yet]) before the authenticateUser is called the User attributes are set to null...



I'm aware that this is a pretty basic question... but are you able to point out where my mistake is?



Thanks.





No comments:

Post a Comment