2013-02-21 56 views
0

我有啊:selectOneMenu,当它的值更改为特定值(比如“2”)时,它假设显示隐藏的字段then(hasn没有被渲染)。注意到它们与托管bean中属性的值相同。h:inputText在更改时不重新显示h:selectOneMenu值,不能按预期工作

<h:outputText value="Function:"></h:outputText> 
<h:selectOneMenu id="funDrp" converter="FunctionConv" value="#{cardBean.card.functionId}"> 
    <f:selectItems value="#{commonData.functions}" var="c" itemLabel="#{c.description}" itemValue="#{c.functionId}" /> 
    <f:ajax render="@form" execute="@form" event="valueChange" /> 
</h:selectOneMenu> 

<h:outputText value=" Profile Id:" rendered="#{(cardBean.card.functionId.functionId==2)}"></h:outputText> 
<h:inputText id="card_refillProfileId"rendered="#{(cardBean.card.functionId.functionId==2)}" label="Refill Profile Id" required="true" value="#{cardBean.card.refillProfileId}"></h:inputText> 

<h:outputText value="Origin Type:" rendered="#{(cardBean.card.functionId.functionId==2)}"></h:outputText> 
<h:inputText id="card_originType" rendered="#{(cardBean.card.functionId.functionId==2)}" label="Origin Node Type" required="true" value="#{cardBean.card.originType}"></h:inputText> 

他们工作正常,如果“卡”对象分配给现有的实体,但如果它是一个新的 - 如预期它不工作。

+0

请详细说明“不工作按预期”的开发者条款。究竟发生了什么(不)? – BalusC 2013-02-21 12:10:00

+0

支持bean的范围是什么? – kolossus 2013-02-21 14:17:53

回答

0

问题是附加到选择菜单的值是“card.functionId”。因此,在检查它之前初始化它。因为我在构建bean之前尝试初始化它,但它没有帮助。

这样做,初始化是通过附加一个“ValueChangeListener”做了selectOneMenu用于

<h:selectOneMenu id="funDrp" converter="FunctionConv" valueChangeListener="#  {cardBean.changeCardFunc}" 
              value="#{cardBean.card.functionId}"> 
              <f:selectItems value="#{commonData.functions}" var="c" 
              itemLabel="#{c.description}" itemValue="#{c.functionId}" /> 
              <f:ajax render="@form" execute="funDrp" event="change" /> 
              </h:selectOneMenu> 

,并在Managed Bean:

public void changeCardFunc(ValueChangeEvent e) { 
// object newFunc= e. 

    if(card!=null){ 
    card.setFunctionId((Function)e.getNewValue()); 
    }else { 
     card= new Card(); 
     card.setFunctionId((Function)e.getNewValue()); 
     System.err.println(e.getNewValue()); 
    } 
} 
-1

尝试

<f:ajax render="card_refillProfileId card_originType" execute="@form" event="valueChange" /> 
+0

我不能呈现不存在的东西http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#AjaxRenderingOfContentWhichIsByItselfConditionallyRendered – 2013-02-21 10:13:46

-1

<f:ajax render="@form" execute="@form" event="valueChange" /> 

应(可省略事件)

<f:ajax render="@form" execute="@form" event="change" /> 

似乎有selectOneMenu.value之间的不匹配,你的转换器和渲染EL。 有两次functionId.functionIdselectOneMenu.value绑定到cardBean.card.functionId。我可能错了,如果functionId是一个函数。

+0

它们工作正常,如果“卡”对象被分配给一个现有的实体它工作正常,但如果它是一个新的它不does – 2013-02-21 10:14:33

+0

如果“卡”对象被分配给一个现有的实体',你是什么意思?你的意思是如果'cardBean.card'被初始化了吗? – 2013-02-21 10:38:40

+0

嗯,我已经初始化它到处,当我声明它和@PostConstructor,但没有解决它 – 2013-02-21 12:01:48

0

这是一个完全预期的行为。使用<f:ajax>,您可以重新呈现DOM中存在的HTML元素,但通过在JSF组件中指定rendered="false",它们不会出现在DOM中。而且,由于阿贾克斯需要找到这个元素通过它的id,并且之后替换它,这个工作显然是无法完成的。

你的工作,但是,可以通过拥抱你的组件在<h:panelGroup id="to-be-rerendered">,它总是呈现在一个容器中(所有这些使用rendered属性),例如,但它的孩子处理 - 没有。因此,如果满足条件,ajax将能够重新渲染面板,该面板将包含您的所有组件,并且它们将出现在您的视图中。

作为一个方面说明,如果您的视图/表单增长,您的ajax行为可能会非常昂贵。因此,如果您只想要重新渲染某些组件,请在您的<f:ajax>标记中指定它:<f:ajax render="to-be-rerendered" />(要渲染的组件的空格分隔ID列表)。

+0

OP使用'render =“@form”',所以我没有看到问题。 – BalusC 2013-02-21 12:09:23

+0

@BalusC我并不是说这是一个问题,但我想指出Marwa的注意力是逃避非民族的渲染,因为它不必要地增加了ajax响应的大小。 – skuntsel 2013-02-21 12:13:26