【发布时间】:2013-02-23 06:35:01
【问题描述】:
编辑 4
我想做的是实现 forgotPassword 页面。例如,我以下面的示例为例,我将用户名保留在会话范围内并不是真正的用户相关问题。
index.xhtml 将是我将输入用户名的忘记密码页面。输入用户名后,我会点击Welcome Me - Action 和chkMe(),我会检查该用户并在他/她的电子邮件ID 和welcome.xhtml 中发送新密码,我会说Hi User ABC, we have sent new password at asdfasdf@dasf.com。
主帖
我正在尝试使用两种情况将数据从一个 bean 打印到另一个。下面是我的代码。
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";
}
}
当我在文本字段中输入文本为Checking 并单击按钮Welcome Me - Plain 时,我在welcome.xhtml 中看到文本为Welcome --Checking-- 文本,但是当我单击Welcome Me - Action 时,我看不到任何文本(我看到Welcome ----)
我不知道为什么会这样。
任何想法/建议为什么会发生这种情况。
编辑 1
我相信这都是?faces-redirect=true造成的,但我必须像不使用?faces-redirect=true一样使用它,地址栏中的URL是以前的URL。
例如如果我在 page1.xhtml 并转到 page2.xhtml,仍然 URL 会显示 page1.xhtml。
所以不知道在这种情况下该怎么办。
编辑 2
好吧,我真正想要做的是忘记密码页面,我将在 index.xhtml 中输入用户名(考虑上面的示例),如果该用户名正确,在welcome.xhtml 中,我将输入Hi User ABC, Please use new password for next login. We have sent you email at blah@blah.com。
RequestScope 运行良好,但问题出在 URL 地址上,因此我添加了 ?faces-redirect=true。但是随着它的重定向,http 会话正在关闭,因此在welcome.xhtml 上,我没有得到任何值(这就是上面发生的情况)。
skuntsel 的另一个解决方案是使用 FlashScope,但问题是当我刷新welcome.xhtml 时,数据消失了, 这让我发疯了。
谁能建议需要做什么?
编辑 3
会话范围的问题如下。
考虑我打开两个选项卡,在两个选项卡上我都有 index.xhtml。在 tab1 上,我输入 Fahim 并单击 Welcome Me - Action。在 tab1 上,welcome.xhtml 出现,我看到文本为 Welcome Fahim。 这太完美了。
现在我来到 tab2,输入名称为XYZ,然后单击Welcome Me - Action,我得到了welcome.xhtml,我看到文本为Welcome XYZ。 这也是完美的。
问题是当我回到 tab1 并刷新页面时。当我刷新 tab1 (welcome.xhtml) 时,我看到 Welcome XYZ 这是错误的,之前它是 Welcome Fahim,它应该是 Welcome Fahim。
【问题讨论】:
-
Fahim,你应该仔细看看 BalusC 在communication in JSF 上的一篇优秀文章。
-
你为什么不直接调用
@takeMeToAnotherPage' instead of@checkMe' 就像#{helloBean.takeMeToAnotherPage}一样。我猜当你像你一样调用一个方法时,页面已经重定向到takeToAnotherPage.因此,您正在尝试显示在@RequestScopedBean的情况下不存在的重定向页面。 -
@Srinivas : 结果还是一样
-
所有人都可能看到 Edit 2 这正是我想要的。它与用户无关,它与忘记会话有关。我使用的示例仅用于演示目的。
-
@FahimParkar 我认为更新后的答案实际上解决了您的问题(以及原来的问题)。
标签: java jsf jsf-2 scope javabeans