I've got a form in a view, for example say:
form.xhtml
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Name" />
<h:inputText value="#{bean.name}" />
<h:outputLabel value="Age" />
<h:inputText value="#{bean.age}"
converter="#{ageConverter}" />
<h:outputLabel value="" />
<h:commandButton action="#{bean.submit}"
value="Submit" />
</h:panelGrid>
</h:form>
Supported by the following bean:
Bean.java
@Named
// Scope
public class Bean implements Serializable {
@Inject private Service service;
private String name;
private int age;
private List<Person> people;
public void submit() {
people= service.getPeople(name, age);
}
// getters & setters for name & age
// getter for people
}
Resulting in a view for people
:
result.xhtml
<h:form>
<h:dataTable value="#{bean.people}"
var="person">
<h:column>
<f:facet name="header">Name</f:facet>
#{person.name}
</h:column>
<h:column>
<f:facet name="header">Day of Birth</f:facet>
#{person.dayOfBirth}
</h:column>
</h:dataTable>
</h:form>
Now obviously the use-case is similar to: - Submit the form using form.xhtml - Get people from service using Bean.java - Show people using result.xhtml
In this example there's still a small part of the puzzle incomplete. For example the scope is decisive in whether or not there are people
at all in the result, furthermore there's no forward (or anything similar) to the resulting page.
Now I'm not sure in what would be the best (or at least a good) way to accomplish this. Here are some ways I've been able to think of:
- Using
@ViewScoped
(JSF2.2) and implicit navigation (returning aString
fromsubmit()
) to navigate to the second page. However this breaks the viewscope (anyway to accomplish this)? - Using
@ViewScoped
and include the right file (form.xhtml or result.xhtml) based onrendered=''
with some EL. This could be done with an Ajax-call on the submit. - Passing the values
name
andage
as GET-parameters on a request to result.xhtml and executing the logic on a@PostConstruct
(though, what if the form is 'huge')? In this case@RequestScoped
would suffice.
My question is, what would be an efficient and good (best) way to accomplish this usecase?
Thanks for your input.