我正在学习 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;
}
}
- 表有三列,第一列是主键,第二列存储用户输入的字符串,第三列存储“测试短语”。
控制器豆:
@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() 中检查数据,然后再将其写入数据库。我的问题是,它始终为空。控制台输出: INFO :string secondcolumn= null 。
secondcolumn 不是由 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 中的条目是“测试短语”。我没有收到任何错误消息,我已尽我所能解决问题,现在我被卡住了。欢迎任何反馈。