f: setPropertyActionListener未设置变量



我尝试过各种不同的方法,但似乎无法设置"selected"变量。

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页面上输出了"selected"变量,它总是返回零。

首先需要更正此导入和此注释:

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

你不需要Named和ManagedBean,你应该有一个或另一个。ManagedBean是由JSF管理的bean,Named是由CDI管理的bean。然后你需要看看你想要的范围是什么,也许@Dependent不适合你想要的。你不应该同时拥有@SessionScoped和@Dependent,这两个作用域都有不同的生命周期。尝试删除@Dependent,它的作用域比@SessionScoped小。如果你删除了@ManagedBean,你需要更改@SessionScoped的导入,它需要是:

import javax.enterprise.context.SessionScoped;

此导入适用于CDI bean,如果您选择只有:

@Named
@SessionScoped

按以下进行尝试

<h:commandButton id="submit" value="Details" action="#{lab3.results(row.carid)}"/>
public String results(int selected) {
    this.selected = selected;
    return "carresults";
}

相关内容

  • 没有找到相关文章

最新更新