【问题标题】:Using JSF 2.0 Flash Scope problem使用 JSF 2.0 Flash Scope 问题
【发布时间】:2011-03-14 10:43:45
【问题描述】:

我在使用基本的 JSF 2.0 程序时遇到了问题!我正在尝试实现 Flash 作用域,我已将信息转移到第二页,但现在无法引用闪现的变量。

这是我的第一个带有 bean 的页面,效果很好:

<?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>Welcome to the JSF PoCs project</title>
 </h:head>
  <h:body>
   <h:form>
     <h1>Welcome to the JSF PoCs project</h1>
     <h:outputText value="Name: "/>
     <h:inputText id="txtName" value="#{registerBean.name}"/>
     <h:outputText value="EMail: "/>
     <h:inputText id="txtEMail"  value="#{registerBean.email}"/> 
     <h:commandButton value="register" action="#{registerBean.register}"/>
     <h:outputText></h:outputText>
    </h:form>
 </h:body>
</html>


import javax.faces.bean.ManagedBean;
@ManagedBean(name="registerBean")
public class RegisterBean {
private String name;
private String email;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String register() {

    JSFUtils.flashScope().put("registerBean", this);
    return "next";
}



}

这就是问题所在!它在heading1标签中显示了正确的数据,但没有用相同的数据设置下一个bean变量?

现在我的问题是,为什么它适用于标题标签而不是设置其他 bean 变量?

<?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"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:f="http://java.sun.com/jsp/jstl/functions">
<h:head>
    <title>Welcome to the JSF PoCs project</title>
</h:head>
<h:body>
    <h:form>
        <c:set target="${confirmBean}" property="name" value="#    {flash.registerBean.name}"/>
        <c:set target="${confirmBean}" property="email"     value="${flash.registerBean.email}"/>
        <h1>Your User Name will be #{flash.registerBean.name} and email will be #    {flash.registerBean.email}</h1>
        <h:commandButton value="confirm" action="#{confirmBean.confirm}"/>
    </h:form>
</h:body>
</html>


import javax.faces.bean.ManagedBean;

@ManagedBean(name="confirmBean")
public class ConfirmBean {
     private String name;
     private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String confirm() {
    JSFUtils.flashScope().put("confirmBean", this);
    return "confirmPage";
}

}

在confirm.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>Welcome to the JSF PoCs project</title>
</h:head>
<h:body>
    You have now registered with:  #{flash.confirmBean.name} and #    {flash.confirmBean.email}<br/>
</h:body>
</html>

提前感谢您的帮助!!!!

【问题讨论】:

  • 嘿,@ViewScope 有什么解决方案吗??

标签: java jsf-2


【解决方案1】:

JSTL &lt;c:set&gt; 标记在构建视图时运行,而不是在呈现或恢复视图时运行。因此,它们在ConfirmBean 中设置仅在请求显示表单中,而不是在请求提交表单中。由于ConfirmBean 是请求范围的,因此所有设置值都将丢失。

我建议仅通过传递参数来替换整个 Flash 范围。

<h:commandButton value="confirm" action="#{confirmBean.confirm}">
    <f:param name="name" value="#{registerBean.name}" />
    <f:param name="email" value="#{registerBean.email}" />
</h:commandButton>

ConfirmBean

@ManagedProperty(value="#{param.name}")
private String name;

@ManagedProperty(value="#{param.email}")
private String email;

【讨论】:

  • 这仍然不起作用,应该有来自 value="#{flash.registerBean.name} 和 value="#{flash.registerBean.email}" 的信息在下一页,但它是空白的。如果我输入类似 value="Michael" 但不像现在这样的东西,我会工作吗?
  • 嗯,我明白了。我更新了答案以采取另一种方法。
  • 我还是不行……不过别担心,我会用 viewscope 代替,谢谢你的帮助!! :)
猜你喜欢
  • 1970-01-01
  • 2011-08-07
  • 2011-07-12
  • 2013-02-21
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-02
  • 2011-04-03
相关资源
最近更新 更多