30

Can somebody recommend any framework to facilitate CRUD development in JSF 2.0?

Aspects I value most:

  • As lightweight as possible; limited dependencies on third party libraries
  • Support for an evolving domain model
  • Limited need for repetitive coding; support for scaffolding and/or metaannotations

Any hints highly appreciated! Yours, J.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jan
  • 9,397
  • 13
  • 47
  • 52
  • Just came across Krank, but it's not very much alive: http://code.google.com/p/krank/ Not JSF2-ready, but I like the ideas. Does this inspire somebody? – Jan Jul 06 '10 at 14:51
  • Why not to use some tool which can generate a "crud application" for you? For example, Netbeans can do that http://netbeans.org/kb/docs/web/jsf20-crud.html – corsair Sep 13 '12 at 12:35

6 Answers6

47

CRUD is indeed a piece of cake using JSF 2.0 provided standard facility: a @ViewScoped bean in combination with a <h:dataTable> basically already suffices. Here's a code example which is shamelessly copied from this article.

Bean:

package com.example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Item> list;
    private Item item = new Item();
    private boolean edit;

    @PostConstruct
    public void init() {
        // list = dao.list();
        // Actually, you should retrieve the list from DAO. This is just for demo.
        list = new ArrayList<Item>();
        list.add(new Item(1L, "item1"));
        list.add(new Item(2L, "item2"));
        list.add(new Item(3L, "item3"));
    }

    public void add() {
        // dao.create(item);
        // Actually, the DAO should already have set the ID from DB. This is just for demo.
        item.setId(list.isEmpty() ? 1 : list.get(list.size() - 1).getId() + 1);
        list.add(item);
        item = new Item(); // Reset placeholder.
    }

    public void edit(Item item) {
        this.item = item;
        edit = true;
    }

    public void save() {
        // dao.update(item);
        item = new Item(); // Reset placeholder.
        edit = false;
    }

    public void delete(Item item) {
        // dao.delete(item);
        list.remove(item);
    }

    public List<Item> getList() {
        return list;
    }

    public Item getItem() {
        return item;
    }

    public boolean isEdit() {
        return edit;
    }

    // Other getters/setters are actually unnecessary. Feel free to add them though.

}

Page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Really simple CRUD</title>
    </h:head>
    <h:body>
        <h3>List items</h3>
        <h:form rendered="#{not empty bean.list}">
            <h:dataTable value="#{bean.list}" var="item">
                <h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
                <h:column><f:facet name="header">Value</f:facet>#{item.value}</h:column>
                <h:column><h:commandButton value="edit" action="#{bean.edit(item)}" /></h:column>
                <h:column><h:commandButton value="delete" action="#{bean.delete(item)}" /></h:column>
            </h:dataTable>
        </h:form>
        <h:panelGroup rendered="#{empty bean.list}">
            <p>Table is empty! Please add new items.</p>
        </h:panelGroup>
        <h:panelGroup rendered="#{!bean.edit}">
            <h3>Add item</h3>
            <h:form>
                <p>Value: <h:inputText value="#{bean.item.value}" /></p>
                <p><h:commandButton value="add" action="#{bean.add}" /></p>
            </h:form>
        </h:panelGroup>
        <h:panelGroup rendered="#{bean.edit}">
            <h3>Edit item #{bean.item.id}</h3>
            <h:form>
                <p>Value: <h:inputText value="#{bean.item.value}" /></p>
                <p><h:commandButton value="save" action="#{bean.save}" /></p>
            </h:form>
        </h:panelGroup>
    </h:body>
</html>

Further, Netbeans has some useful wizards to genreate a CRUD application based on a datamodel.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ah, I looked for that article but couldn't find it. And was lazy to give a complete example myself :) (+1) – Bozho Jul 05 '10 at 16:20
  • Yeah, the example code is appreciated. Still I'm wondering if there isn't a way to generate all this code, driven by a set of annotated Entity objects. – Jan Jul 05 '10 at 16:31
  • The answer doesn't fully fit all requirements I had in mind. Though, it looks like the best answer available, so I'll mark it as accepted. (I'm new to StackOverflow... Is this the right thing to do?) – Jan Jul 07 '10 at 10:43
  • You can also consider using Netbeans' builtin wizards. I edited the answer to include a link. – BalusC Jul 07 '10 at 15:59
  • @Matt: don't think do. Perhaps you just need to add the item to the list? – BalusC Jun 30 '11 at 04:49
  • Ah, nevermind - looks like it's due to a [bug in PrimeFaces 2.2.1 DataTable](http://stackoverflow.com/questions/6422151/). Sorry to bug you! – Matt Ball Jun 30 '11 at 13:31
  • @BalusC , What if I want to create a separate page for editing the item, with the bean being ViewScoped. I currently do the following steps: 1. Put the item in the session 2. Redirect to the edit page. 3. On the edit page, I get the bean from session using ui:repeat or f:param. Is it the right way? –  Oct 15 '11 at 14:05
  • Will this work if i aim to update multiple rows at the same time ? – Abhishek Singh Sep 21 '15 at 09:38
7

JSF 2.0 itself. CRUD is very easy to do with JSF alone - no need for any other framework. You need

  • 1 managed bean (annotated with @ManagedBean)
  • 2 xhtml pages (facelets) - one for list and one for edit/create
  • A <h:dataTable> with anedit link/button, by which you set the current row object in the managed bean (using action="#{bean.edit(currentRowObject)}"). (In JSF 1.2 this was achieved by <f:setPropertyActionListener>)
  • Action methods (void, with no arguments) to handle the operations
  • @PostConstruct to load the data initially.
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Hi Bozho, Thank you for the reply. I've added an additional 'requirement' to the original question: Limited need for manual coding. I understand your answer, but on a large domain model, the manual approach remains cumbersome. I'm wondering if somthing Grails-like exists, but in pure JSF. Thank you , J. – Jan Jul 05 '10 at 16:18
  • 2
    Isn't setPropertyActionListener unneeded in JSF 2.0, as we can pass objects as arguments? – Thorbjørn Ravn Andersen Jul 06 '10 at 06:40
  • @Thorbjørn Ravn Andersen indeed. thanks for reminding me. added to the answer. – Bozho Jul 06 '10 at 07:02
5

I created this one to speed up process of jsf crud application creation: https://github.com/ignl/happyfacescrud Out of box search, lazy data table, viewing/editing, custom components that reduces code dramatically and of course flexible.

Enrique San Martín
  • 2,202
  • 7
  • 30
  • 51
Ignas
  • 141
  • 1
  • 6
  • 1
    Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Nov 05 '12 at 19:25
  • @Ignas Is happyfacescrud can do database reverse engineering for generating jsf pages and its respective beans? – Sam Aug 29 '13 at 17:49
2

I had the same problem as described: Creating as-fast-as-possible CRUD-App in JEE6.

Beautiful Generator found at: http://sourceforge.net/projects/jbizmo/

After defining (Graph-Editor!) your Business-Model/Domain-Model, JBizMo creates the database and a whole CRUD-App out of the Box.

  • i18n, JAAS, also supported
  • Views and Menus are generated
  • ... a bunch of parameters to define ...
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
2

I found this article useful as well:

Conversational CRUD in Java EE 6

http://www.andygibson.net/blog/tutorial/pattern-for-conversational-crud-in-java-ee-6/

By Andy Gibson

Jan
  • 9,397
  • 13
  • 47
  • 52
1

I found an opensource crud generator for JSF+Primefaces

http://minuteproject.wikispaces.com/Primefaces

And also it generate crud for most of the frameworks http://minuteproject.wikispaces.com/

Sam
  • 685
  • 12
  • 34