Question

I have an actionListener within h:commandButton but when pressed error comes out as method not found.

Here's the code:

<h:form id="course">
    <ui:repeat value="#{stationCourses.course}" var="course">
        <h:commandButton class="buttons"
            id="courseID"
            value="#{course.courseName}"
            actionListener="#{stationCourses.courseSelected}"
            style="alignment-adjust: central;
            white-space: pre-line; word-wrap: break-word;"/>
    </ui:repeat>            
</h:form>

and the bean:

import javax.faces.event.ActionEvent;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.List;

@ManagedBean
@RequestScoped
public class StationCourses implements Serializable {

//provide connection to db
private CourseQueries cq = new CourseQueries();
private List<Course> courses = cq.getCourses("station_naas");
private int courseNumber;

public void setCourse()
{
    courses = cq.getCourses("station_naas");
}//end method setCourse

public List<Course> getCourse() {
    return courses;
}//end method getCourseID

public void getCourseSelected(ActionEvent event)
{     
    // course:j_idt6:1:courseID";

    //HTML buttons generate ID like the one above
    //get char at 14 will get the unique button number
    //we get course according to that button and -1 
    //to correct the off by one error
    courseNumber = event.getComponent().getId().charAt(14) - 1;

    System.out.println(courses.get(courseNumber).getCourseID());
}//getCourseSelected

}//end class StationCourses
Was it helpful?

Solution

Change your code as

   <h:form id="course">
                <ui:repeat value="#{stationCourses.course}" var="course">
                    <h:commandButton class="buttons"
                              id="courseID"
                              value="#{course.courseName}"
                              actionListener="#{stationCourses.getCourseSelected}"
                              style="alignment-adjust: central;
                              white-space: pre-line; word-wrap: break-word;"/>
                </ui:repeat>            
            </h:form>  

For actions and actionListeners you have to mention the exact method name unlike variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top