2014-10-16 52 views
0

我有一个使用JSF的XHTML页面。在这个页面上,我有一个输入表单,用于收集调用我的托管bean类(称为AnimalBeanTwo)中的Add()方法所需的参数。当我点击添加按钮来调用方法时,我收到一个错误...JSF托管Bean属性目标无法访问

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 + "]"; 
    } 
} 

我如果需要可以提供任何进一步的代码任何建议表示赞赏。谢谢。

回答

1

尝试初始化AnimalTwo对象在其getter方法是这样的: -

在AnimalTwo类

public AnimalTwo() { 
// empty constructor 
} 

,并在您managedBean编辑getter方法类似这样的写入新的空构造: -

public AnimalTwo getAnimal() { 
    if(animal == null){ 
     animal = new animal(); 
    } 
    return animal; 
} 

我希望这能解决它。