【问题标题】:f:setPropertyActionListener not setting variablef:setPropertyActionListener 未设置变量
【发布时间】:2014-10-16 22:42:42
【问题描述】:

我尝试了各种不同的,但似乎无法设置“选定”变量。

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”变量,它总是返回零。

【问题讨论】:

  • 你试过调试吗?单击按钮时是否调用 setSelected?

标签: jsf managed-bean


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试如下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 2013-03-14
      相关资源
      最近更新 更多