java - 我如何使 JPA POJO 类 + Netbeans 表单很好地协同工作?

标签 java netbeans jpa controller javabeans

我开始使用 netbeans 设计表单来编辑我在我正在编写的一个小应用程序中创建的各种类的实例。基本上,应用程序启动,从数据库中选择一组初始对象并显示在列表中,然后可以选择列表中的项目进行编辑。当编辑器出现时,它具有类中许多数据字段的表单字段。

我遇到的问题是我必须创建一个 Controller 来将每个数据元素映射到正确的表单元素,并创建大量的小转换映射代码行以将数字转换为字符串并设置正确的下拉列表中的元素,然后是另一个过多的代码,用于返回并在单击保存按钮时使用表单中的所有值更新基础对象。

我的问题是;有没有更直接的方式让表单的编辑直接修改我的类实例的内容?我希望能够有一个我可以配置的默认映射“ Controller ”,然后在需要时覆盖特定字段的 getter/setter。理想情况下,将对电话号码、整数、 float 、邮政编码等内容进行标准字段验证...

我不反对自己写这个,我只是想看看它是否已经存在并使用正确的工具来完成正确的工作。

最佳答案

方法有很多种,

JBoss Seam ,例如,使用名为 hbmtemplate 的 Ant(不管您是否知道,NetBeans 在幕后使用 Ant)工具.它是一个基于模板的引擎,可以由用户提供的模板或类来控制。它与 Freemarker 模板(.flt 扩展名)一起生成所有应用程序。如果您想了解 Seam 如何生成其应用程序,请查看 /seam-gen/view。在那里,您可以看到很多 Freemarker 模板。

下面是 Seam 如何生成它的应用程序

<hibernate templatepath="${templates.dir}">
    <jpaconfiguration persistenceunit="${project.name}"/>
    <classpath>
        <dirset dir="${project.home}/exploded-archives">
            <include name="*.war/WEB-INF/classes" if="project.war"/>
            <include name="*.war/WEB-INF/dev" if="project.war"/>
            <include name="*.jar" if="project.ear"/>
        </dirset>
    </classpath>
    <property key="hibernatetool.util.toolclass" value="org.jboss.seam.tool.Util"/>
        <hbmtemplate filepattern="{class-name}List.xhtml" template="view/list.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}.xhtml" template="view/view.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}.page.xml" template="view/view.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}Edit.xhtml" template="view/edit.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}Edit.page.xml"                template="view/edit.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="${action.dir}/{class-name}List.java" template="src/EntityList.java.ftl" destdir="${project.home}/src" foreach="entity">
            <property key="actionPackage" value="${action.package}"/>
        </hbmtemplate>
        <hbmtemplate filepattern="{class-name}List.page.xml" template="view/list.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="${action.dir}/{class-name}Home.java" template="src/EntityHome.java.ftl" destdir="${project.home}/src" foreach="entity">
            <property key="actionPackage" value="${action.package}"/>
        </hbmtemplate>
        <hbmtemplate filepattern="menu.xhtml" template="view/layout/menu.xhtml.ftl"  destdir="${project.home}/view/layout" foreach="entity"/>
    </hibernate>

这里是 一些代码,不是全部,来自 FreeMarket Template view.xhtml.ftl

<#foreach property in pojo.allPropertiesIterator>
    <#if !c2h.isCollection(property) && !isToOne(property) && property != pojo.versionProperty!>
        <#include "viewproperty.xhtml.ftl">
    </#if>
</#foreach>

希望对你有用

关于java - 我如何使 JPA POJO 类 + Netbeans 表单很好地协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2503087/

相关文章:

java - 将 Camel 路由限制为 HTTP 方法

java - Chartboost Android 集成 - 如何监听加载的广告

java - 在 Java 中自动将代码插入到 catch 语句中

Netbeans NoClassDefFoundError

java - JPA 实体管理器未正确注入(inject) - Weblogic

jpa - Spring Data JPA Projection嵌套列表投影接口(interface)

java - Jackson 树结构的序列化/反序列化

java - netbeans 自定义 jax-ws stub

java - 来自单个 validator 的集合验证

java - 为什么 JAVA 中的 BIGInteger 对更高的幂没有响应?