0

我的页面包含一个登录标题,我通过 ui:include 包含该标题。包含的页面包含一个带有 ap:commandButton 的对话框。当用户登录时,包含页面根据更新属性中的@form 正确刷新。我还想更新包含页面之外的组件,该组件应在用户登录时显示一个按钮。包含页面刷新并显示登录用户的名称。但是没有显示主页中的按钮。如果我刷新页面,它会显示。我无法弄清楚我在这里做错了什么。有人有想法么。

标题页还正确显示了 commandLink 组件。但是当点击注销链接时,主页中的按钮并没有被移除。由于 commandLink 不使用 ajax,我假设一个普通的页面 POST 已经完成。哪个应该重新加载整个页面。这在使用 ui:include 引用的页面上不起作用吗?

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

这是包含的 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>
4

2 回答 2

2

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

您必须在始终呈现的父命名容器上调用 AJAX 更新。因此,更新:createform而不是里面的按钮。无论如何,表单总是被渲染。

于 2016-05-17T20:30:18.807 回答
0

我发现了这个问题。在我的命令按钮“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 (loginController) 来检查用户是否已登录。更改为以下工作。

  <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>

注意差异render="#{loginController.login ...}而不是 render="#{recepieController.loggedIn}"

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

于 2016-05-19T17:05:46.753 回答