不要传递值

时间:2014-05-22 02:01:53

标签: java jsf primefaces

我尝试将值传递给其他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>

Bean2

@PostConstruct
public void init() {

   String title = Util.getRequestParameter("value");
   method();//this method need of variable title

}

的Util

public static String getRequestParameter(String name) {
   return FacesContext.getCurrentInstance().getExternalContext()
             .getRequestParameterMap().get(name);
 }

好吧,我不知道为什么,但它总是发送值为null。

有人知道问题是什么吗?

1 个答案:

答案 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