2014-10-16 4 views
2

저는 다양한 방법을 시도했지만 "selected"변수를 설정할 수 없습니다.f : setPropertyActionListener가 변수를 설정하지 않습니다.

자바 빈즈 :

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 : 나는 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> 

및 항상 0을 반환합니다.

+0

디버깅을 시도 했습니까? 버튼을 클릭 할 때 setSelected가 호출됩니까? – Multisync

답변

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보다 작은 범위를 갖습니다. 당신이 @SessionScoped의 수입을 변경해야 @ManagedBean 삭제하면 될 필요가있다 : 당신은 단지에 선택하는 경우

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"; 
} 
관련 문제