推荐的 JSF 2.0 CRUD 框架

发布于 2024-09-08 09:24:40 字数 1536 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倚栏听风 2024-09-15 09:24:40

使用 JSF 2.0 提供的标准工具,CRUD 确实是小菜一碟: @ViewScoped bean 与 基本上已经足够了。这是一个代码示例,无耻地从这篇文章复制而来。

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>

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

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.

只是在用心讲痛 2024-09-15 09:24:40

JSF 2.0 本身。仅使用 JSF 就可以轻松完成 CRUD - 无需任何其他框架。您需要

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

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.
や三分注定 2024-09-15 09:24:40

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.

岁月静好 2024-09-15 09:24:40

我发现这篇文章也很有用:

Java EE 6 中的会话 CRUD

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

<安迪·吉布森

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

怪我鬧 2024-09-15 09:24:40

我遇到了与所描述的相同的问题:Creating as-fast-as-possible CRUD-App in JEE6。

美丽的生成器位于: http://sourceforge.net/projects/jbizmo/

定义后(图-编辑!)您的业务模型/域模型,JBizMo 创建数据库和整个 CRUD 应用程序开箱即用。

  • i18n,JAAS,还支持
  • 生成视图和菜单
  • ...一堆要定义的参数...

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 ...
卷耳 2024-09-15 09:24:40

我找到了一个用于 JSF+Primefaces 的开源 CRUD 生成器

http://Minuteproject.wikispaces.com/Primefaces

而且它为大多数框架生成 CRUD http://Minuteproject.wikispaces.com/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文