Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF and JPA best practice

Tags:

jpa

jsf

I've a little experience with JSF and I would like to know the best practice to build a classic crud application with search capabilities. In particular I 'm in doubt about this last one; I've a form where the user type the object id; I've all my entities mapped with jpa and the relating facade session bean. But in the jsf page how could I read the parameter, call the finder method and then display the object properties? Shall I use a servlet that get the object and store it in the request scope and access to that eith JSF EL?

Thank you in advance

like image 471
Neos76 Avatar asked Feb 11 '11 13:02

Neos76


1 Answers

You don't need a servlet. Just inject the session facade / EJB by @EJB in managed bean and call it in the bean action method and then display it the usual JSF/EL way in the result page.

View:

<h:form>
    <h:inputText value="#{bean.search}" />
    <h:commandButton value="search" action="#{bean.submit}" />
</h:form>
<h:panelGroup rendered="#{not empty bean.result}">
    <p>#{bean.result.someProperty}</p>
</h:panelGroup>

Model:

@ManagedBean
@RequestScoped
public class Bean {

    private String search; // +getter +setter
    private Data result; // +getter

    @EJB
    private DataFacade dataFacade;

    public void submit() {
        result = dataFacade.find(search);
    }

}
like image 117
BalusC Avatar answered Oct 14 '22 14:10

BalusC