【问题标题】:JSF/JPA value binding expression not setting entity bean's propertyJSF/JPA 值绑定表达式未设置实体 bean 的属性
【发布时间】:2013-09-13 02:08:41
【问题描述】:

我正在学习 JSF/EJB,但遇到了问题。

我正在尝试编写一个从用户获取字符串并将该字符串存储到数据库的代码。

这是我的代码: 实体 bean:

@Entity

public class TestTable implements Serializable {


private static final long serialVersionUID = 1L;

public TestTable() {
    super();
}

@Id
@GeneratedValue
private int firstcolumn;
private String secondcolumn;

private String testphrase = "test phrase";


public String getTestphrase() {
    return testphrase;
}
public void setTestphrase(String testphrase) {
    this.testphrase = testphrase;
}
public int getFirstcolumn() {
    return firstcolumn;
}
public void setFirstcolumn(int firstcolumn) {
    this.firstcolumn = firstcolumn;
}
public String getSecondcolumn() {
    return secondcolumn;
}
public void setSecondcolumn(String secondcolumn) {
    this.secondcolumn = secondcolumn;
}



}
  • 表有三列,第一列是主键,第二列存储用户输入的字符串,第三列存储“测试短语”。

控制器 bean:

@Named
public class TestController  implements Serializable {

private static final long serialVersionUID = 1L;

@EJB
DataAccess dacc;


@Inject
TestTable testTable;

public TestController()
{

}



public TestTable getTestTable() {
    return testTable;
}



public void setTestTable(TestTable testTable) {
    this.testTable = testTable;
}



public void test()
{

    System.out.println("string secondcolumn= "+ testTable.getSecondcolumn());
    dacc.addtodb(testTable);


}

}
  • 我使用了 System.out.println("string secondcolumn= "+ testTable.getSecondcolumn());在方法 test() 中检查数据,然后再将其写入数据库。我的问题是,它始终为空。控制台输出: 信息 :string secondcolumn= null .

第二列不是由 JSF 中的值绑定表达式设置的。

现在,JSF:

        <h:outputText value="Second column:">
        </h:outputText>


        <h:inputText label="Second column" value="#{testController.testTable.secondcolumn}">                
        </h:inputText>


        <h:outputText value="#{testController.testTable.getTestphrase()}">
        </h:outputText>

        <h:commandButton action="#{testController.test}" value="Save">
    </h:commandButton>

我检查了数据库并正在添加行。 SECONDCOLUMN 列中的条目为 NULL。

TESTPHRASE 中的条目是“测试短语”。我没有收到任何错误消息,我已尽我所能解决问题,现在我被卡住了。欢迎任何反馈。

【问题讨论】:

    标签: java jsf cdi


    【解决方案1】:

    您的问题是您正在注入一个实体类。最好的方法是使用 new 关键字手动初始化它,从数据库中检索实体等。一种方法是在 CDI bean 中使用 @PostConstruct 方法:

    @Named
    //here you should define the scope of your bean
    //probably @RequestScoped
    //if you're working with JSF 2.2 there's already a @ViewScoped
    public class TestController  implements Serializable {
    
        private static final long serialVersionUID = 1L;
        @EJB
        DataAccess dacc;
        //this musn't be injected since it's not a business class but an entity class
        //@Inject
        TestTable testTable;
    
        public TestController() {
        }
    
        @PostConstruct
        public void init() {
            //basic initialization
            testTable = new TestTable();
        }
    
        //rest of your code...
    }
    

    通过此更改,JSF 将能够将 &lt;h:form&gt; 中的值设置到有界字段中。请注意,JSF 代码只会根据您的 EL 中的定义调​​用必要的 getter 和 setter,它不会创建有界字段的新实例。生成视图时调用 getter,向服务器提交表单时调用 setter。

    更多信息:

    【讨论】:

    • 只是一个快速的跟进,唯一可以@Inject实体的时间是如果你为他们创建一个生产者。如果您让 CDI 管理它们,它们将无法持久化。 CDI 和 JPA 都依赖代理或字节码操作,它们不能很好地协同工作。
    • @LightGuard 你能举个例子吗?只是为了用更准确的信息改进答案。
    • 生产者的例子?
    • @LightGuard 我没有注入 JPA 实体、EJB 和 CDI 托管 bean。如果您可以展示预期的示例,请执行此操作,甚至发布您自己的答案。
    • 嗯,经典的例子是当前登录的用户实体。像@Inject @LoggedIn User currentUser 这样的东西。如果您在没有生产者的情况下执行此操作,则 1)可能不会填写您想要的信息,并且 2)由于上述原因而无法持久化。
    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多