How do you do paged lists in JavaServer Faces? - java

I have a JSF application that I am converting over to use webservices instead of straight up database queries. There are some extremely long lists that before could be returned easily with a simple SQL query. I'd like to figured out how implement the paging using JSF/web services. Is there a good design pattern for doing paged web services?
If it matters, I'm currently using the Apache MyFaces reference implementation of JSF with the Tomahawk extensions (a set of JSF components created by the MyFaces development team prior to its donation to Apache).

It depends on whether you want to do client-side or server-side paging. If server side, your web services will have to include a couple of additional parameters (e.g. "startFrom" and "pageSize") which will let you specify which 'page' of the data to retrieve. Your service will probably also need to return the total result size so you can generate a paging control.
If you decide that's too much effort you can do client-side paging in your backing bean (or get a component to do it for you), however it's not recommended if you're talking about thousands of objects!

I like Seam's Query objects: http://docs.jboss.com/seam/2.1.0.BETA1/reference/en-US/html_single/#d0e7527
They basically abstract all the SQL/JPA in a Seam component that JSF can easily use.
If you don't want to use Seam and/or JPA you could implement a similar pattern.

Trinidad has a table component that supports paging, which may help. It is not ideal, but works well enough with Seam, as described in Pete Muir's Backing Trinidad's dataTable with Seam blog post.
If you don't find a JSF component you like, you'll need to write your own logic to set parameters for limit and offset in your EJB-QL (JPA) queries.

We used the RichFaces library Datatable: http://livedemo.exadel.com/richfaces-demo/richfaces/dataTable.jsf?tab=usage
It's quite simple, and if you're not using RichFaces already, it's really easy to integrate with MyFaces.

If you are getting all the results back from the webservice at once and can't include pagination into the actual web service call, you can try setting the list of items to a property on a managed bean. Then you can hook that up to the "value" attribute on a Tomahawk dataTable:
http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_dataTable.html
and then you can use a Tomahawk dataScroller to paginate over the list of items stored in that property. Here is the reference for that component, it works well with the dataTable component:
http://myfaces.apache.org/tomahawk-project/tomahawk/tagdoc/t_dataScroller.html
You can include this inside the header/footer facets of the dataTable or as a separate compoment (you will need to specify the id of the dataTable in the 'for' attribute of the dataScroller.
There's other neat things you can do with the dataTable like sorting and toggling details for each row, but that can be implemented once you get the basic pagination working.
Hope that helps!

Related

How to pass data from database to JSF page?

I want to pass data from database to JSF page as a table. I know that I can use ArrayList but I need more efficient way because I have database tables with 2000+ rows and more. I;m not sure but the ArrayList will consume too much memory. Is there more memory efficient way to send the data to the JSF page?
P.S maybe using pagination?
You can start with a simple PrimeFaces - DataTable - Pagination Example... its really simple and efficient...
DataTable has built-in support for ajax pagination.
Then if you will deal with really huge data you can take a look at a more advanced example.
Take a look at the PrimeFaces DataTable - Lazy Loading example
DataTable has built-in support to deal with huge datasets. In order to enable lazy loading, a LazyDataModel needs to be implemented to query the datasource when pagination, sorting, filtering or live scrolling happens
And here is a link to all PrimeFaces Datatable ability's
With Richfaces you can use database backed pagination. There is a bunch of example/tutorials if you search for richfaces pagination. Here is one that looks promising:
http://www.myjeeva.com/2011/04/do-jsfrichfacesseam-lazydynamic-data-loading-pagination/

POJO or DTO approach

I am developing a new web application with Struts2, Spring and Hibernate as its core building blocks.
We have created POJO classes with respect to hibernate mapping files.There will be some inputs from users which needs to be updated in to the underlying database
e.g registration or updation.
We have few option like creating new POJO/DTO for action classes which will be filled by the Struts2 and than we can transfer them to the service layer where we can convert those DTO to the respected hibernate POJO else we can expose same POJO to struts2 so that the framework can fill them with the user input and we need not to do the work for conversion and creating extra set of classes.
Application will be not big in size and will have a medium size application tag.
My question is what is the best way to transfer this user input to underlying hibernate layer to perform data base specific work.
Thanks in advance
I'd prefer the "DTO" approach in this case since you then can validate the input first and trigger updates only when wanted.
However, you could use detached entities as DTOs and reattach them when you want to create or update them. If you don't want the web part of your application to depend on Hibernate and/or JPA you might need to create another set of classes (unless you don't use a single annotation).
You'll get both answers on this.
With Struts 2 I tend to use normal S2 action properties to gather form values/etc. and use BeanUtils to copy them to the Hibernate objects. The problem with exposing the Hibernate objects to the form, like with ModelDriven etc. is that you need to define whitelists/blacklists if you have columns that should not be set directly by the user. (Or handle the problem in a different way.)
That said, I'm not fundamentally opposed to the idea like a lot of people are, and they're arguably correct.

Spring Roo Presentation and Model binding

I am starting on Springsource using Spring Roo.
Q1. Am I correct to say that Spring Roo automates a lot of tedium out of Spring?
I am asking that because when I follow non-roo tutorials on Spring, I am reading that I have to perform a significant amount of configuration and stubs which would have been done by roo. I don't see why, prior to roo, would Spring help reduce my work load.
Q2. Is it necessary to define entities using the namespace ~.domain.whatever. Is it necessary to have "domain"? Can I do
entity --class ~.profile.PostalAddress
? Is Spring sensitive to the word "domain"?
Q3. I would like to define an entity
Employee { long id, string name, string project, date startdate}
I would like to bind Employee to a presentation panel where
id is a hidden field,
name is displayed non-editable field,
project is displayed editable field populated with the current value in the db,
startdate is not even sent to the browser.
How do I accomplish that in Roo? If I cannot do that in Roo, is there a way I could muck around with the Spring generated innards to accomplish it and how easy would it be?
Q4. Spring roo generates a standard template web interface. But I do not wish to use the interface. I just want roo to generate RESTful panels for me, which I could associate with a GWT menu item.
The reason is I need to use GWT/SmartGWT navigation widgets and when the appropriate data manipulation item is called thro gwt/smartgwt, I would like to call/enable that Spring generated data panel RESTfully.
How do I get roo, or subsequently Spring, to generate a RESTful data panel for each of my entity without using Spring or roo's generated (useless/unattractive) default navigation side panes? Would I have to manually modify those roo generated files? If so, how and what?
In short, I want to use the convenience of roo's help in generating lots of presentation-validation-model bound modules but use my own GWT/Smart GWT to navigate to them. I am NOT asking about roo+gwt.
Q1: It is true that prior to Roo you would have to write quite a few configuration files for a Spring application. However, without Spring you would have to write even more configuration and, worse, Java code to glue it all together.
Q2: Roo is agnostic of the word "domain". You can call your packages any way you want. Personally I prefer the sub-package "model" for domain classes.
Can't say anything about Q3 and Q4, sorry.
Caveat - I am not a ROO expert.. just getting started myself
Q3:
You can find out how to do all of that here :
http://static.springsource.org/spring-roo/reference/html/base-web.html#jsp-views
ID : you'll need to import the standard jstl tags to support <frm:hidden> (Roo doesn't have this by default)..
Name: You can render the Name field as plain text, or as a disabled text box -- if the latter, you can add disabled=true to the tag, I believe.
Project : Roo will do what you need for Project out of the box (although, if "Project" is another entity in a Many-Many relationship, it will create a Select Box by default, with all the possible Projects, and the current one selected)
StartDate : set "render='false'" on the roo-generated tag and it will not even output the html for that field.
In all cases, Roo will re-write your input fields unless you change the tags Z field to z="user-managed".
I would probably keep the original ROO tag, change it to render="false" z="user-managed" and add your own beneath it to do what you really want.
Q4
Not familiar with GWT, but have you considered the roo web gwt

Java-Spring-Hibernate: How can I save a filter and, afterward, how could I use it?

I receive a task for a project which uses Spring, Hibernate and Wicket.
In a particular HTML page I must have the possibility to create a filter(set the name of the filter and its parameters). I must create a list of filters in this way.
In the same time, I must have the possibility to edit and delete the filter, and of course, to use that filter.
Any ideas? How could I do this?
if you're looking for a component to edit a list of items, then this might help: Building a ListEditor form component
you can use spring hibernate (or other ORM's) support to retrieve the data for your model without too much hassle.

Automatic entity mapping similar to O/R-mapping with JSF?

With JPA I do not need to code the SQL for every new attribute as the o/r-mapping is being done automatically.
As I am new to JSF, i am wondering whether there is a similar possiblity with JSF?
I do not want to add new code to a jsf datatable every time I change something at the corresponding entity.
JSF provides a way to manage events and the lifecycle of a request and its linked objects. Its always possible to use any ORM framework with it because JSF doesn't play directly with the database (it doesn't even know about it). Hibernate + JSF is a very common combination.
But if you are asking about using JSF managed beans with a JPA framework, have a look at Seam: http://www.seamframework.org/.
I do not want to add new code to a jsf datatable every time I change something at the corresponding entity.
If you want a dynamic datatable you will probably have to use an add-on to core JSF. I use IceFaces and it works very well. You can use the <ice:columns> component to generate columns dynamically. We use this to display the results of a query which may return different columns.

Categories