Edit 4
What I wanted to do is implement forgotPassword page. For example purpose I have taken below example and it is not real user related question where I will keep username in session scope.
index.xhtml would be forgotPassword page where I would be entering username. After entering username, I would be clicking Welcome Me - Action and in chkMe(), I would be checking that user and send new password on his/ her email id and in welcome.xhtml, I would be saying Hi User ABC, we have sent new password at [email protected].
Main Post
I am trying to print data from one bean to another with two cases. Below is the code I have.
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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me - Plain" action="welcome"></h:commandButton>
<h:commandButton value="Welcome Me - Action" action="#{helloBean.chkMe()}"></h:commandButton>
</h:form>
</h:body>
</html>
welcome.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://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
<h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
<h4>Welcome --#{helloBean.name}--</h4>
</h:body>
</html>
HelloBean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class HelloBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String chkMe() {
return takeMeToAnotherPage("welcome");
}
public String takeMeToAnotherPage(String linkToGo) {
return linkToGo + "?faces-redirect=true";
}
}
When I enter text as Checking in textfield and click button, Welcome Me - Plain, I see text as Welcome --Checking-- text in welcome.xhtml, however when I click Welcome Me - Action, I don't see any text (I see as Welcome ----)
I don't know why this is happening.
Any idea/ suggestion why this is happening.
Edit 1
I believe this is all causing because of ?faces-redirect=true, but I have to have use this as if I don't use ?faces-redirect=true, the URL in address bar is previous url.
e.g. If I am on page1.xhtml and I go to page2.xhtml, still URL will say page1.xhtml.
So not sure what to do in such case.
Edit 2
Well, what I actually want to do is forgotPassword page where I will enter username in index.xhtml (considering above example) and if that username is correct, on welcome.xhtml, I will have Hi User ABC, Please use new password for next login. We have sent you email at [email protected].
RequestScope was working perfectly, but the problem was with URL address and hence I added ?faces-redirect=true. But as its redirect, http session is closing and hence on welcome.xhtml, I don't get any value (which is what is happening above).
Another solution from skuntsel was to use FlashScope, but there again the problem is when I refresh welcome.xhtml, data is gone, which drives me crazy.
Can anyone suggest for what needs to be done?
Edit 3
The problem in session scope is as below.
Consider I open two tab and on both tab I have index.xhtml. On tab1 I enter Fahim and clicked Welcome Me - Action. On tab1, welcome.xhtml comes and I see text as Welcome Fahim. This is perfect.
Now I come to tab2, and enter name as XYZ, and clicked Welcome Me - Action I get welcome.xhtml and I see text as Welcome XYZ. This is also Perfect.
The problem is when I comes back on tab1 and refresh the page. When I refresh the tab1 (welcome.xhtml), I see Welcome XYZ which is wrong as earlier it was Welcome Fahim and it should be Welcome Fahim.
@takeMeToAnotherPage' instead of@checkMe' like#{helloBean.takeMeToAnotherPage}.My Guess would be when you call a method like you did , the page already redirected when it comes totakeToAnotherPage. So, You are trying to display redirected page which doesn't exist in the case of@RequestScopedBean. - SRy