-2

I am trying to learn web development with JSF by myself. There are planty of sources to learn about this and that but I am having hard time binding them all together.

Imagine I have a database with a table called user which have columns: id, name, surname. I am using JPA in my project so I have a class with @Entity annotation, which is mapped to this class. I also have an index.xhtml, in which I have a Registiration Form with username and password fields. So when the user clicks the REGISTER button, I should check if a user with same username exists, if not, I should register the user and redirct the user to welcome.xhtml. If registiration is unsuccessful, then user should stay in index.xhtml.

My questions are:

I have an index.xhtml, and a UserEntity.Java. But what else? Do I need a RegistirationFormBean which is a @ManagedBean? And this RegistirationFormBean will have a registerUser method with. Then what? Do I have a RegistirationFormControllerBean? Should it be a ManagedBean as well? Or do I need a UserRegistirationBean? UserRegistirationService?

So how do I create the MVC properly?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

1

You just need one bean - but there is caveat, you shouldn't use ManagedBean annotation anymore and move forward to CDI technology, so just use @Named. The you will have getter and setter for your entity so you can work with it on the page. And finally the registration method, ti can look like

@Named
//@RequestScoped is the default
public class MyBean {

    @Inject //you can inject other beans or EJBs
    private UserDaO userDao; //this class will handle DB operations for user

    private YourUserEntity entity;

    public void register() {
        if(!userDao.isUserExists(entity)) {
            userDao.save(entity);
        }
    }
}
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • Thanks. So UserDao.Java has a UserEntity.Java? And how is MyBean connected to the form in my xhtml page? – Koray Tugay May 13 '13 at 08:08
  • https://netbeans.org/kb/docs/javaee/cdi-intro.html - this tutorial will get you started, you are asking about most basic stuff – Petr Mensik May 13 '13 at 08:12
  • I do not see any MVC or database connection in this tutorial, thanks though. – Koray Tugay May 13 '13 at 08:18
  • You are dealing here with different technologies, first learn basics of JSF and CDI (these two are ment to work together) - you can use the link I gave you or some other tutorial. After you now what's going on, then take a look at JPA or Hibernate tutorial and try to wire up these things together – Petr Mensik May 13 '13 at 08:27
  • And for MVC take a look here - http://stackoverflow.com/questions/5104094/what-components-are-mvc-in-jsf-mvc-framework – Petr Mensik May 13 '13 at 08:28
  • Thanks. I want to use Tomcat as server so I guess I will be using JSF Managed Beans. – Koray Tugay May 13 '13 at 08:36
  • You still can use CDI - http://www.lucubratory.eu/getting-weld-cdi-into-tomcat-7/ – Petr Mensik May 13 '13 at 08:46