0

Here is what I am trying to do:

<!DOCTYPE html>
<html
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"

        >

    <h:head>
        <title>Sign Up Page</title>
    </h:head>

    <h:body>

            <div class="well" style="margin-top: 20px">
                <h2>Please Sign Up</h2>
            </div>

            <h:form>
                <h:outputLabel for="username">User name:</h:outputLabel>
                <h:inputText id="username" value="#{signUpPage.user.username}" />
                <h:outputLabel for="password">Password:</h:outputLabel>
                <h:inputSecret id="password" value="#{signUpPage.user.password}" />
                <h:outputLabel for="passwordAgain">Password Again:</h:outputLabel>
                <h:inputSecret id="passwordAgain" value="#{signUpPage.passwordAgain}" />
                <br />
                <h:commandButton value="Submit" styleClass="btn btn-primary"
                        action="#{signUpPage.addUser}"/>
            </h:form>

    </h:body>

</html>

And the managed bean:

@ManagedBean
@ViewScoped
public class SignUpPage {

    @Inject
    private User user;

    @Inject
    private UserDao userDao;

    private String passwordAgain;

    public String getPasswordAgain() {
        return passwordAgain;
    }

    public void setPasswordAgain(String passwordAgain) {
        this.passwordAgain = passwordAgain;
    }

    public User getUser() {
        return user;
    }

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

    public void addUser(){
        userDao.persistUser(this.user);
    }
}

And the Entity:

@Stateless
@Entity
@Table(name = "user")
public class User {

    private int id;
    private String username;
    private String password;

    @Column(name = "id")
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

    @Column(name = "username")
    public String getUsername(){
        return username;
    }

    public void setUsername(String username){
        this.username = username;
    }

    @Column(name = "password")
    public String getPassword(){
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }

}

So this does not work, because user resolves to be null.

Is there anyway to do this?

The exception I am getting is:

javax.servlet.ServletException: /signupPage.xhtml @34,82 value="#{signUpPage.user.username}": Target Unreachable, 'null' returned null
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • How would you like to inject user? From where? You need to create "empty" user, then let it be filled from the form on submit. Then you can handle business logic on your own. – Piotr Gwiazda Sep 02 '13 at 18:03
  • I do not know really. From the pool. Some empty User object is created by the Server? This is at least my expectation.. – Koray Tugay Sep 02 '13 at 18:05

1 Answers1

3

You need to create empty user entity by yourself. Generally when using JSF lazy creating and loading in getters is the best way. Just change the getter:

public User getUser() {
    if (user == null) {
        user = new User();
    }
    return user;
}

Also remember about validation.

//edit You've probably mixed annotations, read https://stackoverflow.com/a/12012663/221951

Community
  • 1
  • 1
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • But why does the container not create one for me? It is an EJB? – Koray Tugay Sep 02 '13 at 18:14
  • If it is JEE6 container may create User entity as it is a `@Stateless` - which makes no sense. Either way `@ManagedBean` is a different context than CDI. Read http://stackoverflow.com/a/12012663/221951 – Piotr Gwiazda Sep 02 '13 at 18:16
  • Try using `@Named` instead `@ManagedBean`. CDI should create beans with `@Dependent` scope automatically. – Piotr Gwiazda Sep 02 '13 at 18:25