我尝试将值传递给其他bean,但是不起作用,显然不会调用get和set方法..请参阅我的代码:
<h:panelGrid columns="3">
<p:outputLabel
value="Label: "/>
<p:inputText id="inputtext" value="#{bean1.title}"/>
<p:button
value="Submit"
outcome="${pageContext.request.contextPath}/pages/formBean2">
<f:param name="value" value="#{bean1.title}"/>
</p:button>
@PostConstruct
public void init() {
String title = Util.getRequestParameter("value");
method();//this method need of variable title
}
public static String getRequestParameter(String name) {
return FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get(name);
}
好吧,我不知道为什么,但它总是发送值为null。
有人知道问题是什么吗?答案 0 :(得分:1)
我不会深入研究这个问题,因为你在JSF上使用了错误的方法,我认为我可以指导你朝着正确的方向发展。
您应该从<{em> title
访问Bean1
的Bean2
值。
你需要像这个例子一样使用ManagedProperty
(未经测试):
public class Bean2 {
@ManagedProperty //maybe youll need (value="#{bean1}")
private Bean1 bean1;
// Getter and setter for Bean1
}
在您发出请求后,JSF框架会在title
上应用您的Bean1
值。它属于那个bean,所以请保留它。
在Bean2
的行动中,您可以从getTitle()
引用Bean1
。标题将在那里,容器将把Bean1
的依赖注入Bean2
。
这是正确的方法。
我建议您在应用程序之外做一个快速示例,这样您就可以看到它有效,之后您可以适应您的项目。
请参阅此示例:Injecting Managed Beans In JSF 2.0
<强>更新强>
请注意,如果您使用的是ViewScoped
bean,则需要保留在同一个应用程序(也称为页面)中才能访问注入的bean。如果您需要在视图之间导航,则需要Faces Flow。