使用JSF 2.0 Flash Scope问题

发布于 2024-10-21 23:37:14 字数 3688 浏览 2 评论 0原文

我在使用基本的 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";
}



}

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

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

<?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";
}

按下确认.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>

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

I'm having trouble with a basic JSF 2.0 program! I'm trying to implement Flash scope, I've gotten the information carried over to the second page, but am now having trouble with referencing the flashed variables.

Here is my first page with its bean, this works fine:

<?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";
}



}

This is where the problem is! It displays the correct data in the heading1 tags but does not set the next beans variable with the same data?

Now my question is, why does it work for a heading tag but not to set another beans variables?

<?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";
}

}

After the button is pressed on the confirm.xhtml it directs to this page:

<?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>

Thanks for any help in advance!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-10-28 23:37:14

JSTL 标记在构建视图时运行,而不是在呈现或恢复视图时运行。因此,它们仅在显示表单的请求中在 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;

The JSTL <c:set> tags runs when the view is built, not when the view is rendered or restored. Thus, they are set in the ConfirmBean only in the request to display the form, not in the request to submit the form. Since the ConfirmBean is request scoped, all set values are lost.

I'd suggest to replace the whole Flash scope by just passing parameters.

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

with the following in ConfirmBean:

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

@ManagedProperty(value="#{param.email}")
private String email;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文