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


豆2

@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);
 }


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

有人知道问题是什么?

最佳答案

我不会深入探讨这个问题,因为您在JSF上使用了错误的方法,我想我可以指导您朝着正确的方向发展。

您应该从title访问Bean1Bean2值。

您需要像下面的示例一样使用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

10-07 19:27
查看更多