推荐的JSF 2.0 CRUD框架

Jan*_*Jan 30 java jsf crud jsf-2

有人可以推荐任何框架来促进JSF 2.0中的CRUD开发吗?

方面我最重视:

  • 尽可能轻巧; 对第三方库的有限依赖
  • 支持不断发展的领域模型
  • 对重复编码的需求有限; 支持脚手架和/或元注释

任何提示高度赞赏!你的,J.

Bal*_*usC 47

使用JSF 2.0提供的标准工具CRUD确实是小菜一碟:@ViewScoped<h:dataTable>基本已经足够了.这是从本文中无耻地复制的代码示例.

豆:

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.

}
Run Code Online (Sandbox Code Playgroud)

页:

<!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>
Run Code Online (Sandbox Code Playgroud)

此外,Netbeans有一些有用的向导可以基于数据模型生成CRUD应用程序.


Boz*_*zho 8

JSF 2.0本身.CRUD很容易单独使用JSF - 不需要任何其他框架.你需要

  • 1个托管bean(带注释@ManagedBean)
  • 2个xhtml页面(facelets) - 一个用于列表,一个用于编辑/创建
  • <h:dataTable>带有edit链接/按钮的A ,用于在托管bean中设置当前行对象(使用action="#{bean.edit(currentRowObject)}").(在JSF 1.2中,这是通过以下方式实现的<f:setPropertyActionListener>)
  • 操作方法(void没有参数)来处理操作
  • @PostConstruct 最初加载数据.

  • 在JSF 2.0中不需要setPropertyActionListener,因为我们可以将对象作为参数传递吗? (2认同)

小智 5

我创建了这个来加速jsf crud应用程序创建的过程:https://github.com/ignl/happyfacescrud 开箱即用搜索,懒惰数据表,查看/编辑,自定义组件,可以显着减少代码,当然还有灵活性.