I am learning JSF/EJB and I have run into a problem.
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:
@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:
@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()); in method test() to check data before it is written to database. My problem is, it is always null. Output in console : INFO :string secondcolumn= null .
secondcolumn is not set by value binding expression in JSF.
Now, 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.
Entries in TESTPHRASE are "test phrase". 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.