我有一个使用JSF的XHTML页面。在这个页面上,我有一个输入表单,用于收集调用我的托管bean类AnimalBeanTwo中的Add()方法所需的参数。当我单击添加按钮调用该方法时,我收到一个错误。。。
value="#{animalBeanTwo.animal.name}":目标不可访问,"animal"返回null。
JSF/xhtml表单如下所示:
<h:form>
<table id ="addRecordTable">
<tr>
<td><h:outputText value="Enter Name: " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.name}" id="name" label="name" required="true" requiredMessage="Name is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td><h:outputText value="Enter Age: "/></td>
<td><h:inputText value="#{animalBeanTwo.animal.age}" id="age" label="age" required="true" requiredMessage="Age is required."></h:inputText></td>
</tr>
<tr>
<td><h:outputText value="Enter Type : " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.type}" id="type" label="type" required="true" requiredMessage="type is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td><h:outputText value="Enter Weight : " /></td>
<td><h:inputText value="#{animalBeanTwo.animal.weight}" id="weight" label="weight" required="true" requiredMessage="Weight is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td></td>
<td><h:commandButton value="Add" action="#{animalBeanTwo.add}">
</h:commandButton></td>
</tr>
</table>
</h:form>
我的托管bean如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
@ManagedBean
public class AnimalBeanTwo {
@ManagedProperty(value = "#{animalTwo}")
protected AnimalTwo animal;
public AnimalTwo getAnimal() {
return animal;
}
public void setAnimal(AnimalTwo animal) {
this.animal = animal;
}
public List<AnimalTwo> getAnimals() {
List<AnimalTwo> animals = new ArrayList<AnimalTwo>();
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = null;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
String url = "jdbc:db2://uk01dv11db.travel.oag.com:60080/SAMPLE";
con = DriverManager.getConnection(url,"","");
System.out.println("Connection completed (Select All).");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String stm = "SELECT ID,Name, Type, Weight,Age, ID FROM Animals";
try {
pst = con.prepareStatement(stm);
pst.executeQuery();
rs = pst.getResultSet();
while (rs.next()) {
int age = rs.getInt("Age");
String type = rs.getString("Type");
String name = rs.getString("Name");
int id = rs.getInt("ID");
int weight = rs.getInt("Weight");
AnimalTwo a1 = new AnimalTwo(id, type, name, age, weight);
animals.add(a1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return animals;
}
public void add() {
PreparedStatement ps = null;
Connection con = null;
String url = "jdbc:db2://uk01dv11db.travel.oag.com:60080/SAMPLE";
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
con = DriverManager.getConnection(url, "", "");
String sql = "INSERT INTO Animals(Age, Type, Name, Weight) VALUES(?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, animal.getAge());
ps.setString(2, animal.getType());
ps.setString(3, animal.getName());
ps.setInt(4, animal.getWeight());
ps.executeUpdate();
System.out.println("Data Added Successfully Into Database");
} catch (Exception e) {
System.out.println(e);
System.out.println("exception here");
}
}
}
这是我的AnimalTwo类:
package com.oag.jsf;
public class AnimalTwo
{
private int id;
private String type;
private String name;
private int age;
private int weight;
public AnimalTwo(int id, String type, String name, int age, int weight) {
super();
this.id = id;
this.type = type;
this.name = name;
this.age = age;
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "DB2Animal [id=" + id + ", type=" + type + ", name=" + name
+ ", age=" + age + ", weight=" + weight + "]";
}
}
如果需要,我可以提供任何进一步的代码。欢迎提出任何建议。谢谢
尝试在其getter方法中初始化AnimalTwo
对象,如下所示:-
在AnimalTwo类中编写新的空构造函数
public AnimalTwo() {
// empty constructor
}
在您的managedBean编辑getter方法中,如下所示:-
public AnimalTwo getAnimal() {
if(animal == null){
animal = new animal();
}
return animal;
}
我希望这能解决它。