I have a JSF page (index.xhtml) that shows students read from the database and displays their names and other data in the data table. There is a link "UPDATE" that calls loadStudent() from the index.html managed bean class. The method should put Student object in the request map and redirect to a update page (updatestudent.xhtml). Update page should display the data read from the Student object.
Student class
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String studentName;
private String studentLastname;
// getters and setters
I have tried to put a Student object in a request map and then to redirect.
public void loadStudent(Student student) throws IOException {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
requestMap.put("student", student)
FacesContext.getCurrentInstance().getExternalContext().redirect("updatestudent.xhtml");
It redirects successfully but the student info is not displayed in the updatestudent.xhtml page.
updatestudent.xhtml managed bean
@Named
@ViewScoped
public class UpdateStudent implements Serializable {
private Student student;
@PostConstruct
private void init() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
student = (Student) requestMap.get("student");
}
//getters and setters
If I make loadStudent() method like below then it redirects successfully and the student data is read in the updatestudent.xhtml
public String loadStudent(Student student) throws IOException {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> requestMap = externalContext.getRequestMap();
requestMap.put("student", student)
FacesContext.getCurrentInstance().getExternalContext().redirect("updatestudent.xhtml");
return "updatestudent";
I would not like to use the second example with the return keyword but would like to redirect programmatically and with the Student object in the request map. Is that somehow possible?