JSF组件不是从包含页面中的对话框中的p:commandButton中呈现的



我的页面包含一个登录头,我通过ui:include将其包含在内。包含的页面包含一个带有p:commandButton的对话框。当用户登录时,include页面会根据update属性中的@form正确刷新。我还想更新包含页面之外的组件,当用户登录时,该组件将显示一个按钮。包含页面将刷新,并显示登录用户的名称。但是主页面上的按钮没有显示。不过,如果我刷新页面,它就会显示出来。我不明白我在这里做错了什么。任何人都有任何想法。

标题页还正确显示commandLink组件。但是,当单击注销链接时,主页中的按钮不会被删除。由于commandLink不使用ajax,所以我认为已经完成了正常的页面POST。这将重新加载整个页面。这在一个已经被ui:include引用的页面上不起作用吗?

登录页面使用会话范围的backingbean。主页面是视图范围的。

以下是包含的xhtml(login.xhtml):

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <div style="width:100%;font-size:12px;line-height:20px;background-color:#b0d9e6;color:white">
<h:form>
<h:message id="top_msg"></h:message>
    <h:panelGrid width="100%" columns="3" columnClasses="none,right1,right1">
       <h:outputLink rendered="#{!loginController.loggedIn}" styleClass="text-align:right;" value="javascript:void(0)" onclick="PF('dlg').show();" title="login">
             <p:outputLabel>Login</p:outputLabel>
        </h:outputLink>

    <h:commandLink rendered="#{loginController.loggedIn}" action="#{loginController.logout}" styleClass="text-align:right;" >
         <h:outputLabel>Logout</h:outputLabel>
    </h:commandLink>
    <p:growl id="growl" sticky="true" showDetail="true" life="3000" />
    <p:dialog header="Login" widgetVar="dlg" resizable="false">
        <h:panelGrid columns="2" cellpadding="5">
            <h:outputLabel for="username" value="Username:" />
            <p:inputText id="username" value="#{loginController.username}" required="true" label="username" />
            <h:outputLabel for="password" value="Password:" />
            <p:password id="password" value="#{loginController.password}" required="true" label="password" />
            <f:facet name="footer">
                <p:commandButton value="Login" 
                        update="@form :createform:createbutton"
                        actionListener="#{loginController.login}"
                        oncomplete="handleLoginRequest(xhr, status, args)" >
                </p:commandButton>
            </f:facet>  
        </h:panelGrid>
    </p:dialog>
    </h:panelGrid>
<script type="text/javascript">
    function handleLoginRequest(xhr, status, args)

</script>
</h:form>
</div>
</ui:composition>
...

这个包含在以下主页中:

...
<ui:include src="login.xhtml" />
   <h:form id="createform">
   <h:panelGroup id="createbutton" layout="block">
     <p:commandButton id="createnew"
        ajax="false" 
        action="#{recepieController.createNewRecipes}" 
        value="Create new recipe" 
        rendered="#{recepieController.loggedIn and !recepieController.create and !recepieController.viewOnly and !recepieController.edit}"
        accesskey="s">
      </p:commandButton>
 </h:panelGroup>     
  </h:form>

不能重新渲染未渲染的组件。如果部分渲染了一个按钮,但该按钮未被渲染,则无法对该按钮调用更新,因为它不存在于DOM中。

您必须调用父命名容器上的AJAX更新,该容器总是呈现的。因此,更新:createform而不是里面的按钮。无论怎样,形式总是呈现出来的。

我发现了这个问题。在我的命令按钮"createnew"中,我使用了错误的值在.上进行渲染

 <p:commandButton id="createnew"
    ajax="false" 
    action="#{recepieController.createNewRecipes}" 
    value="Create new recipe" 
    rendered="#{recepieController.loggedIn and !recepieController.create and !recepieController.viewOnly and !recepieController.edit}"
    accesskey="s">
  </p:commandButton>

它应该使用我的会话范围的bean(loginroller)来检查用户是否登录。

  <h:panelGroup id="createbutton">
     <p:commandButton id="createnew"
        ajax="false" 
        action="#{recepieController.createNewRecipes}" 
        value="Create new recipe" 
        rendered="#{loginController.loggedIn and !recepieController.create and !recepieController.viewOnly and !recepieController.edit}"
        accesskey="s">
      </p:commandButton>
    </h:panelGroup>

请注意区别rendered="#{loginController.login…}而不是rendered="#{recepieController.loggedIn}"

recepieController还有一个我设置的loggedIn属性,但由于页面没有重新发布,我想登录时该属性的值不会更改。然而,我相信我测试过在登录对话框的p:commandButton中使用ajax="false",我想这应该会重置我的loggedIn属性的视图范围版本。我不完全理解为什么这不起作用。

最新更新