I have tried various different was but cannot seem to set the "selected" variable.
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>
I have outputted the "selected" variable on the carresults.xhtml page and it always returns zero.
first you need to correct this imports and this annotations:
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
You don't need Named and ManagedBean, yous should have one or the other. ManagedBean are bean managed by JSF, Named are beans managed by CDI. Then you need to see what is the scope you want, maybe @Dependent is wrong for what you want. You shouldn't have @SessionScoped and @Dependent at the same time, both are scopes with different lifecycles. Try deleting @Dependent, it has a smaller scope than @SessionScoped. If you delete @ManagedBean you need to change the import of @SessionScoped, it needs to be:
import javax.enterprise.context.SessionScoped;
This import is for CDI beans, if you choose to only have:
@Named
@SessionScoped