2011-03-14 39 views
2

我遇到了基本的JSF 2.0程序问题!我试图实现Flash范围,我已经将信息传递到第二页,但现在在引用闪存变量时遇到了问题。使用JSF 2.0 Flash范围问题

这是我与它的豆第一页,这工作得很好:

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

感谢提前任何帮助!!!!

+0

哎你得到与'@ ViewScope'任何解决方案? – 2012-03-14 15:42:15

回答

3

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

这仍然不起作用,下一页应该有来自value =“#{flash.registerBean.name}和value =”#{flash.registerBean.email}“的信息,但它是空白的。如果我把价值=“迈克尔”,但不喜欢它现在是什么? – user647646 2011-03-14 12:46:51

+0

嗯,我明白了。我更新了答案采取另一种方法。 – BalusC 2011-03-14 14:05:05

+0

我仍然不工作...但不要担心我是将与Viewscope一起工作,谢谢你的帮助!! :) – user647646 2011-03-15 05:15:02