简体   繁体   English

不要通过传递值

[英]do not to pass a value with <f:param in jsf

I am try to pass a value to other bean, but does not work, apparently the get and set methods are not called.. see my code: 我试图将一个值传递给其他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 Bean2

@PostConstruct
public void init() {

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

}

Util UTIL

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

Well, i dont know why but it always sends the value null. 好吧,我不知道为什么,但是它总是发送空值。

someone has an idea of ​​what the problem is? 有人知道问题是什么?

I wont dive into the issue since you are using a wrong approach on JSF and I think I can guide you in the right direction. 我不会深入探讨这个问题,因为您在JSF上使用了错误的方法,我想我可以指导您朝着正确的方向发展。

You should access the title value of Bean1 from the Bean2 . 您应该 Bean2访问Bean1title值。

You need to use a ManagedProperty like this example (not tested): 您需要像以下示例一样使用ManagedProperty (未经测试):

public class Bean2 {

    @ManagedProperty //maybe youll need (value="#{bean1}")
    private Bean1 bean1;

    // Getter and setter for Bean1

}

After you fire your request the JSF framework will apply your title value on the Bean1 . 触发请求后,JSF框架会将您的title值应用于Bean1 It belongs to that bean, so keep it there. 它属于那个bean,所以将其保留在那里。

In your action in Bean2 you can then make a getTitle() from Bean1 reference. Bean2的操作中,您可以从Bean1引用中获取一个getTitle() The title is going to be there, and the container is going to inject the dependency from Bean1 into Bean2 . 标题将在那里,容器将把依赖从Bean1注入Bean2

This is the right way to do it. 这是正确的方法。

I recommend you to do a quick example outside your application so you can see that it works, and afterwards you can adapt to your project. 我建议您在应用程序外部做一个简单的示例,以便可以看到它的工作原理,然后可以适应您的项目。

See this example: Injecting Managed Beans In JSF 2.0 请参见以下示例: 在JSF 2.0中注入托管Bean

UPDATE UPDATE

Notice that if you are using ViewScoped beans you need to stay in the same application (aka page) in order to access injected beans. 请注意,如果您使用的是ViewScoped bean,则需要停留在同一应用程序(即页面)中才能访问注入的bean。 If you need to navigate between views, you gonna need Faces Flow . 如果您需要在视图之间导航,则需要Faces Flow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM