2014-10-16 116 views
2

我已經嘗試了各種不同的是,但似乎無法設置「選定」變量。f:setPropertyActionListener沒有設置變量

的JavaBean:

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.Arrays; 
import javax.annotation.Resource; 
import javax.inject.Named; 
import javax.enterprise.context.Dependent; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.sql.DataSource; 

@Named(value = "lab3") 
@Dependent 
@ManagedBean 
@SessionScoped 
public class Lab3 { 

public Lab3() { 
} 

@Resource (name="jdbc/sample") // This is the JNDI name 
private DataSource ds; 

private ArrayList<Cars> c = new ArrayList<>(); 

public ArrayList<Cars> getC() { 

    // Declare the JDBC objects. 
    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 

    try { 

     // Establish the connection 
     connection = ds.getConnection("app", "app"); 

     // Create and execute an SQL statement that returns some data. 
     String SQL = "SELECT * FROM cars"; 
     statement = connection.createStatement(); 
     resultSet = statement.executeQuery(SQL); 

     // Iterate through the data in the result set and each column 
     while (resultSet.next()) { 
      c.add(new Cars(resultSet.getInt("CARID"), 
        resultSet.getString("CARMAKE"), 
        resultSet.getString("CARMODEL"), 
        resultSet.getInt("CARYEAR"))); 
     } 
    } // Handle any errors that may have occurred. 
    catch (SQLException e) { 
     System.out.println(Arrays.toString(e.getStackTrace())); 
    } 
    finally 
    { 
     try 
     { 
      if (resultSet != null) 
       resultSet.close(); 
      if (statement != null) 
       statement.close(); 
      if (connection != null) 
       connection.close(); 
     } 
     catch (Exception ex) { 
       System.out.println ("Exception cleaning up Database objects " + 
            ex.getMessage()); 
     } 
    } 
    return c; 
} 

public void setC(ArrayList<Cars> c) { 
    this.c = c; 
} 

    private int selected; 

/** 
* Get the value of selected 
* 
* @return the value of selected 
*/ 
public int getSelected() { 
    return selected; 
} 

/** 
* Set the value of selected 
* 
* @param selected new value of selected 
*/ 
public void setSelected(int selected) { 
    this.selected = selected; 
} 

private ArrayList<Mileage> m = new ArrayList<>(); 

public ArrayList<Mileage> getM() { 

    // Declare the JDBC objects. 
    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 

    try { 

     // Establish the connection 
     connection = ds.getConnection("app", "app"); 

     // Create and execute an SQL statement that returns some data. 
     String SQL = "SELECT * FROM mileage where mileagecarid = " + selected; 
     statement = connection.createStatement(); 
     resultSet = statement.executeQuery(SQL); 

     // Iterate through the data in the result set and each column 
     while (resultSet.next()) { 
      m.add(new Mileage(resultSet.getInt("MILEAGEID"), 
        resultSet.getInt("MILEAGESTART"), 
        resultSet.getInt("MILEAGEEND"), 
        resultSet.getDouble("MILEAGEGASUSED"))); 
     } 
    } // Handle any errors that may have occurred. 
    catch (SQLException e) { 
     System.out.println(Arrays.toString(e.getStackTrace())); 
    } 
    finally 
    { 
     try 
     { 
      if (resultSet != null) 
       resultSet.close(); 
      if (statement != null) 
       statement.close(); 
      if (connection != null) 
       connection.close(); 
     } 
     catch (Exception ex) { 
       System.out.println ("Exception cleaning up Database objects " + 
            ex.getMessage()); 
     } 
    } 
    return m; 
} 

public void setM(ArrayList<Mileage> m) { 
    this.m = m; 
} 

public String results() { 
    return "carresults"; 
} 
} 

的index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <title>Lab3</title> 
    </h:head> 
    <h:body> 
     <h:outputStylesheet library="css" name="style.css" /> 
     <h:form> 
      <h:dataTable id="dbresults" value="#{lab3.c}" var="row" > 
       <h:column> 
        <f:facet name="header" >Make</f:facet> 
        <h:outputText value="#{row.carmake}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Model</f:facet> 
        <h:outputText value="#{row.carmodel}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Year</f:facet> 
        <h:outputText value="#{row.caryear}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Details</f:facet> 
        <h:commandButton id="submit" value="Details" action="#{lab3.results}" > 
         <f:setPropertyActionListener target="#{lab3.selected}" value="#{row.carid}" /> 
        </h:commandButton> 
       </h:column> 
      </h:dataTable> 
     </h:form> 
    </h:body> 
</html> 

carresults.xhtml:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <title>Lab3</title> 
    </h:head> 
    <h:body> 
     <h:outputStylesheet library="css" name="style.css" /> 
     <h:outputText value="#{lab3.selected}" ></h:outputText> 
     <h:form> 
      <h:dataTable id="dbresults" value="#{lab3.m}" var="row" > 
       <h:column> 
        <f:facet name="header" >Start<br />(km)</f:facet> 
        <h:outputText value="#{row.mileagestart}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >End<br />(km)</f:facet> 
        <h:outputText value="#{row.mileageend}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Trip<br />(km)</f:facet> 
        <h:outputText value="#{row.trip}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Gas Used<br />(L)</f:facet> 
        <h:outputText value="#{row.mileagegasused}"> 
        </h:outputText> 
       </h:column> 
       <h:column> 
        <f:facet name="header" >Fuel Economy<br />(L/100km)</f:facet> 
        <h:outputText value="#{row.litre}"> 
        </h:outputText> 
       </h:column> 
      </h:dataTable> 
     </h:form> 
    </h:body> 
</html> 

我已輸出的頁carresults.xhtml的 「選擇」 變量和它總是返回零。

+0

您是否嘗試過調試?當你點擊一個按鈕時setSelected被調用? – Multisync 2014-10-17 05:46:48

回答

0

首先需要糾正這種進口,這個註釋:

import javax.inject.Named; 
import javax.enterprise.context.Dependent; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@Named(value = "lab3") 
@Dependent 
@ManagedBean 
@SessionScoped 

你不需要命名和ManagedBean,你要跟應該有一方或另一方。 ManagedBean是由JSF管理的bean,名稱是由CDI管理的bean。 然後你需要看看你想要的範圍是什麼,也許@Dependent對於你想要的是錯誤的。您不應該同時擁有@SessionScoped和@Dependent,它們都是具有不同生命週期的範圍。 嘗試刪除@Dependent,它的範圍小於@SessionScoped。如果刪除@ManagedBean您需要更改@SessionScoped的進口,它需要:

import javax.enterprise.context.SessionScoped; 

此導入是CDI豆,如果你選擇只有:

@Named 
@SessionScoped 
0

嘗試爲低於

<h:commandButton id="submit" value="Details" action="#{lab3.results(row.carid)}"/> 

public String results(int selected) { 
    this.selected = selected; 
    return "carresults"; 
}