从JSF页面设置Managed Bean属性的值并在bean的方法中使用它 [英] Setting Managed Bean attribute's value from JSF page and using it in bean's methods

查看:209
本文介绍了从JSF页面设置Managed Bean属性的值并在bean的方法中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewScoped托管Bean.在我的.xhtml页面中,我想设置bean的属性值,并在同一bean的方法中使用它. 我设法从jsf页面设置了值,但是当我想以某种方法使用它时,属性的值不是我之前设置的值.

I have a ViewScoped Managed Bean. In my .xhtml page I want to set bean's attribute's value and use it in methods in the same bean. I managed to set the value from jsf page, but when i want to use it in some method the value of an attribute is not the value i have set before.

说明(xhtml): 这种形式的命令链接设置了属性的值.而且工作正常.另外,当单击命令链接时,将显示第二种形式.

Description (xhtml): In this form there is a command link which sets the value of an attribute. And it is working fine. Also, as command link is clicked, second form is being showed.

<h:form>
  <h:commandLink value="Set" >
     <f:setPropertyActionListener target="#{bean.attribute}" value="true" />
     <f:ajax execute="@this" />
  </h:commandLink>
</h:form>

此表单执行使用以前设置的属性值的方法,但该值不是true,而是false.

This form executes method that uses attribute's value set before, but the value is not true, its false.

<h:form>
    <h:commandButton id="submit" value="Execute" action="#{bean.execute}" />
</h:form>

Bean:

public void execute(){
    if(isAttribute())
        ---do something---
}

问题是:为什么execute()不能正确读取属性值?

当我使用一种表格时,它工作正常.但是我需要它们采用单独的形式.

When I use one form, it's working fine. But I need them to be in separated forms.

推荐答案

我无法确定您是否在两个请求中都停留在同一页面上.如果这样做,viewScope甚至应以两种不同的形式工作.如果要从一个视图导航到另一个视图,则将创建另一个viewScope并释放当前视图.

I can't determine of you stay on the same page with both requests. If you do, viewScope should work even in two different forms. If you are navigating from 1 view to another, another viewScope will be created and you will loose the current one.

您可以使用java或通过注释backingNean在sessionScope中设置值.但是,您的backingBean中的所有内容都会变为sessionScoped,并且可能不需要.

You could set the value in the sessionScope with java or by annotating the backingNean. But then everything in your backingBean becomes sessionScoped and that might not be needed.

您还可以使用类似弹簧的流量示波器.

You could also use a spring-like flow scope.

使用Java进行操作的示例:

Example to do it with java:

public void callThisAfterFirstClick() {
   Faces.setSessionAttribute(attribute, true)
}

public void callThisAfterSecondClick() {
   Faces.getSessionAttribute(attribute);
}

这篇关于从JSF页面设置Managed Bean属性的值并在bean的方法中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆