简体   繁体   English

JSF / JPA值绑定表达式未设置实体bean的属性

[英]JSF/JPA value binding expression not setting entity bean's property

I am learning JSF/EJB and I have run into a problem. 我正在学习JSF / EJB,但遇到了问题。

I am trying to write a code that takes a string from user and store that string to database. 我正在尝试编写一个从用户那里获取一个字符串并将该字符串存储到数据库的代码。

Here's my code: Entity bean: 这是我的代码:实体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;
}



}
  • Table has three columns, first column is primary key, second column stores string entered by user and third column stores "test phrase". 该表具有三列,第一列是主键,第二列存储用户输入的字符串,第三列存储“测试短语”。

Controller bean: 控制器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);


}

}
  • I used System.out.println("string secondcolumn= "+ testTable.getSecondcolumn()); 我使用了System.out.println(“ string secondcolumn =” + testTable.getSecondcolumn()); in method test() to check data before it is written to database. 在方法test()中检查数据,然后再将其写入数据库。 My problem is, it is always null. 我的问题是,它始终为null。 Output in console : INFO :string secondcolumn= null . 在控制台中输出:INFO:string secondcolumn = null。

secondcolumn is not set by value binding expression in JSF. JSF中的值绑定表达式未设置secondcolumn。

Now, 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>

I checked database and rows are being added. 我检查了数据库,并添加了行。 Entries in Column SECONDCOLUMN are NULL. 列SECONDCOLUMN中的条目为NULL。

Entries in TESTPHRASE are "test phrase". TESTPHRASE中的条目是“测试短语”。 I get no error messages and I have tried everything I can to solve the problem and now I am stuck. 我没有收到任何错误消息,我已经尽力解决了问题,但现在我陷入了困境。 Any feedback are welcome. 欢迎任何反馈。

Your problem is that you're injecting an entity class. 您的问题是您要注入实体类。 The best will be initializing it manually using the new keyword, retrieving the entity from database, etc. One way to do this would be using a @PostConstruct method in your CDI bean: 最好的办法是使用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...
}

With this change, JSF will be able to set the values from the <h:form> into the bounded fields. 通过此更改,JSF将能够将<h:form>的值设置为有界字段。 Note that JSF code will just invoke the necessary getters and setters accordingly to the defined in your EL, it won't create a new instance of the bounded fields. 注意,JSF代码只会根据EL中定义的内容调用必要的getter和setter,而不会创建有界字段的新实例。 The getters are invoked when generating the view and the setters when submitting the form to the server. 生成视图时调用getter,将表单提交给服务器时调用setter。

More info: 更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用托管Bean绑定属性数据表@ JSF - Binding a Property Data Table @ JSF with a Managed Bean 无法刷新注入JSF 2 Managed Bean的JPA 2实体 - Unable to refresh JPA 2 Entity injected into a JSF 2 Managed Bean JPA - 实体的属性类型取决于列值 - JPA - An entity's property type depends on a column value 以编程方式添加 JSF 组件而不绑定到支持 bean 属性 - Programmatically adding JSF components without binding to backing bean property JPA 2,JSF,设置OneToOne映射实体问题的ID - JPA 2, JSF, setting id of OneToOne mapped entity problem 在页面加载期间从托管bean设置jsf InputSecret的值 - Setting the value for jsf InputSecret during page load from the managed bean 在托管Bean JSF 2.2中将属性值设置为String - setting attribute value to String in managed Bean JSF 2.2 使用ref-bean设置spring bean属性值 - Setting spring bean property value using ref-bean 通过GlassFish3.1中的EJB SessionBean在JSF bean中获取JPA实体时出错 - Error when getting JPA Entity in JSF bean via EJB SessionBean in GlassFish3.1 如何在 jpa 实体的属性上检索 DEFAULT CURRENT_TIMESTAMP 指定的最新(当前)时间值? - How to retrieve the newest(current) time value specificd by DEFAULT CURRENT_TIMESTAMP on jpa entity's property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM