【问题标题】:JSF, AJAX not renderingJSF,AJAX 不呈现
【发布时间】:2013-01-22 13:47:46
【问题描述】:

为什么在这段代码中选择一个项目后,城市组件没有被渲染?

<h:form prependId="false">
<h:panelGrid columns="1">
<h:selectOneMenu value="#{enumBeanStatus.selectedRegion}">
<f:selectItems id="selectItem" value="#{enumBean.regions}" var="region" itemLabel="#{region.label}"/>
<f:ajax listener="#{enumBean.loadCities}" render="cities"/>
</h:selectOneMenu>
<h:outputText id="cities" value="#{enumBean.cities}"/>
</h:panelGrid>
</h:form>

它确实发送了一个带有所选区域的 POST,模型得到正确更新,但 &lt;h:outputText&gt; 组件未呈现。

其中一个支持 bean:

@Named
public class EnumBean {

private List<Region> regions;
private List<City> cities;

@Inject 
EnumBeanStatus enumBeanStatus; //This one is CDI @ApplicationScoped & @Named




// Code...

public void loadCities(){
setCities(City.getCitiesByRegion(enumBeanStatus.getSelectedRegion()));
}

// Getters and Setters
}

【问题讨论】:

  • 请注意,在这种情况下,POST 之后的 GET 不是预期的,也不会出现在任何 AJAX 响应中。当您更新整个页面时,您只需要在 POST 之后获取 GET - 它不是自动的,您必须返回“viewName.jsf?faces-redirect=true”。
  • @elias 那么,更新的信息会在 POST 响应中返回吗?
  • 是的,它包含在 POST 请求的响应中。

标签: jsf jsf-2 selectonemenu


【解决方案1】:

&lt;h:form&gt; 中删除prependId="false"。它会阻止 &lt;f:ajax&gt; 根据相对客户端 ID 解析正确的组件。

另见:

【讨论】:

  • EnumBean 在调用 loadCities() 后被销毁。做了一个愚蠢的假设,即它将是@ApplicationScoped,因为它正在注入该范围的bean。在这种情况下 prependId 根本没有改变。很抱歉造成混乱:(
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 2012-12-01
  • 2011-05-30
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多