我开始使用JSF,我正在尝试使用用户在表单中输入的详细信息将记录添加到数据库中。我的XHTML页面包含此表单。
<h:form>
<table id ="addRecordTable">
<tr>
<td><h:outputText value="Enter Name: " /></td>
<td><h:inputText value="#{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="#{animal.age}" id="age" label="age" required="true" requiredMessage="Age is required."></h:inputText></td>
</tr>
<tr>
<td><h:outputText value="Enter Breed : " /></td>
<td><h:inputText value="#{animal.breed}" id="breed" label="breed" required="true" requiredMessage="Breed is required.">
<f:validateLength minimum="2" maximum="15"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td></td>
<td><h:commandButton value="Add" action="#{animalBean.add}">
<f:setPropertyActionListener target="#{animalBean.animal.age}" value="#{animal.age}" />
<f:setPropertyActionListener target="#{animalBean.animal.name}" value="#{animal.name}" />
<f:setPropertyActionListener target="#{animalBean.animal.breed}" value="#{animal.breed}" />
</h:commandButton></td>
</tr>
</table>
</h:form>
我也有这些java Managed Bean类:
import javax.faces.bean.ManagedBean;
@ManagedBean
public class Animal {
private int id;
private String breed;
private int age;
private String name;
public Animal(int id, int age, String breed, String name) {
this.breed = breed;
this.age = age;
this.id = id;
this.name = name;
}
public Animal()
{
this.breed= "";
this.age = 0;
this.id = 0;
this.name= "";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getBreed() {
return breed;
}
public int getAge() {
return age;
}
@Override
public String toString()
{
return "Animal [id=" + id + ", breed=" + breed + ", age=" + age
+ ", name=" + name + "]";
}
}
和....
import javax.faces.bean.ManagedBean;
@ManagedBean
public class AnimalBean
{
protected Animal animal = new Animal();
public List <Animal> getAnimals() {
List <Animal> animals = new ArrayList <Animal> ();
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = null;
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\lwilson\\Animals.accdb";
try {
con = DriverManager.getConnection(url);
System.out.println("Connection completed (Select All).");
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
String stm = "SELECT Breed,Age,Name, ID FROM Animals";
try {
pst = con.prepareStatement(stm);
pst.executeQuery();
rs = pst.getResultSet();
while (rs.next()) {
int age = rs.getInt("Age");
String breed = rs.getString("Breed");
String name= rs.getString("Name");
int id = rs.getInt("ID");
Animal a1 = new Animal(id,age, breed, name);
animals.add(a1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return animals;
}
public void add() {
File db = new File("C:\\Users\\lwilson\\Animals.accdb");
if (!db.exists()) {
System.out.println("file not found");
}
PreparedStatement ps = null;
Connection con = null;
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\lwilson\\Animals.accdb";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "", "");
String sql = "INSERT INTO Animals(Age, Breed, Name) VALUES(?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, animal.getAge());
ps.setString(2, animal.getBreed());
ps.setString(3, animal.getName());
ps.executeUpdate();
System.out.println("Data Added Successfully Into Database");
} catch (Exception e) {
System.out.println(e);
System.out.println("exception here");
}
}
我的问题是在setPropertyActionListener中,目标没有将Animal对象字段设置为表单中的值。我收到一条错误消息,指出目标无法访问创建的动物对象的字段。然后将这些字段传递给add()方法。
任何帮助将不胜感激。感谢。
答案 0 :(得分:1)
您可以使用Animal
注释将AnimalBean
班级对象注入@ManagedProperty
班级
@ManagedBean
public class AnimalBean {
@ManagedProperty
protected Animal animal;
// getter and setter
// Here your other methods
}
在这种情况下无需使用
<f:setPropertyActionListener target="#{animalBean.animal.age}" value="#{animal.age}" />
<f:setPropertyActionListener target="#{animalBean.animal.name}" value="#{animal.name}" />
<f:setPropertyActionListener target="#{animalBean.animal.breed}" value="#{animal.breed}" />
您将使用Animal
课程getter
和setter
获取您的媒体资源。
答案 1 :(得分:0)
看起来应该是这样的:
1)托管bean类中动物对象的getter和setter
2)通过#{animalBean.animal.value}
以表格形式提及动物对象3)当您发布表单时,动物对象将自动更新。属性动作侦听器是不必要的。