1

I have used mainly the first option, but I came upon the second option, and it seems to do everything faster with less code. Which way is better for creating forms using JSF? Is there a better option for creating form for an application with many server and client side validations? I really do not like to have a constructor like new Person(). What would you advise me? Thank you.

Option 1:

View:

<h:inputText value=#{bean.fisrtName}/>
<h:inputText value=#{bean.lastName}/>
<h:commandButton action=#{bean.submit()}/>

Bean

private firstName;
private lastName;

public String submit(){
    Person person = new Person(firstName, lastName);
    // do something with person
}

Option 2:

View

<h:inputText value=#{bean.person.fisrtName}/>
<h:inputText value=#{bean.person.lastName}/>
<h:commandButton action=#{bean.submit()}/>

Bean

private Person person

@PostConstruct
public void init(){
    person = new Peson();
}

public String submit(){    
    // do something with person
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Pavel
  • 140
  • 2
  • 10
  • So what's wrong with option 2, besides all the typos and missing quotes? – Gimby Jul 18 '14 at 14:26
  • Option 2 is better, especially if you have more than one entity-type instances in the bean and more fields in the entities. –  Jul 18 '14 at 22:03

1 Answers1

0

The way you delevop your managed beans depends on the deepness you put into the MVC approach provided by JSF. If you want to develop a fullworthy JSF application (based on MVC grounds) then you'll basically have, as a starting point:

  • Facelet page as the view: it represents information and handles user actions;
  • Managed bean as the controller: it processes actions and delegates rendering to the right view;
  • Entity as the model: it holds all the data.

In a small beginner-style application you can mix the model with the controller but as the application grows management of such beans could be troublesome. In real-world applications managed bean typically contains relevant entities a its members.

So, option 2 is a clearer MVC approach with a better separation of concerns.

For a bit wider picture consult What components are MVC in JSF MVC framework?.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67