문제

I'm wondering if there is a common pattern for the following scenario.

Let's say I have one JSF page backed with one request scoped bean. I want to fech all data rows from a database table when a user enters this page. The same JSF page contains a form to provide query criteria. When the user provides query criteria and submits the form, I want to display the result on the same page, too.

The suitable place to fetch all rows at page entry is the @PostConstruct method. This is a nice place to do that since additional (injected) request parameters are already available here and can be used in the query. However, parameters submited from the form are not available yet. They can be accessed in the action method.

If the user queries the database table using the form criteria the database will be queried twice in this case. The request scoped bean will be recreated and @PostConstruct method fetching all rows will be called prior to the form action method fetching what the user wants.

Of course I could redirect the form result to another JSF page backed by a different bean with DB query only in the action method. But is there a way to accomplish fetching only what is needed with one JSF page and one managed bean?

도움이 되었습니까?

해결책

What you should use is a combination of ViewScoped managed bean and ajax engine. You're right about using @PostConstruct method to fetch all initial rows of your data table. Any further requests from your query form will not trigger this method again. Instead, you only need to make an ajax call to sort out the entries in your data table and update it at the end of the call. It would be something like this:

<h:dataTable id="myTable">
   ...
</h:dataTable>

<h:form>
   ...
   <h:commandButton actionListener="#{viewScopedBean.sortEntries}">
       <f:ajax render="myTable" execute="myForm" />
   </h:commandButton>
</h:form>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top