[英]JSF 2.0: Delay rendering (composite) component's contents until AJAX-call re-renders it
[英]JSF 2.0 AJAX re-renders more than the IDs I specify. Not sure if I have a fundamental misunderstanding or just a bug
当我单击 logOutButton 时,它似乎也在重新呈现 ajaxR 元素。 我希望它只重新呈现我在 f:ajax 呈现属性中的 ID。 或者它可能正在重新呈现整个页面。 问题是,为什么它呈现 ajaxR 元素和/或整个页面,而不仅仅是我指定的 ID。
为确保我清楚:当我单击注销按钮时,由于 rendered="#{userIdBean.isLoggedIn}" 而未呈现 ajaxR 元素。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<h:head>
<title>SandBox</title>
</h:head>
<h:body>
<h1>SandBox</h1>
<h:form id="form1">
Registration:<br></br>
Email: <h:inputText value="#{regBean.email}"></h:inputText><br></br>
User Id: <h:inputText value="#{regBean.userId}"></h:inputText><br></br>
Password: <h:inputText id="passR" value="#{regBean.password}"></h:inputText><br></br>
<h:outputText rendered="#{userIdBean.isLoggedIn}" id="nodeL" value="Good Luck: #{userIdBean.userId}" style="font-weight:bold" /><br></br>
<h:commandButton value="Submit" type="submit" action="#{regBean.storeUserId}" />
<h:commandButton id="logOutButton" rendered="#{userIdBean.isLoggedIn}" value="Log Out" type="submit" action="#{loginBean.logoutUser}" >
<f:ajax event="keyup" render="nodeL logOutButton"/>
</h:commandButton>
<br></br>
<h:outputText rendered="#{userIdBean.isLoggedIn}" value="AJAX Response: #{userIdBean.userId}"></h:outputText>
</h:form>
</h:body>
</html>
这是 bean 代码的一个片段。
public String logoutUser(){
userIdBean.setIsLoggedIn(false);
userIdBean.setUserId(null);
return null;
}
你在代码中有两个错误:
像<h:commandButton>
这样的UICommand
组件中的<f:ajax event>
必须设置为action
。 您也可以完全不使用event
属性,然后它将默认为合理的默认值(这是命令组件的action
和输入组件的valueChange
)。
<h:commandButton id="logOutButton" rendered="#{userIdBean.isLoggedIn}" value="Log Out" type="submit" action="#{loginBean.logoutUser}" > <f:ajax render="nodeL logOutButton"/> </h:commandButton>
<f:ajax render>
客户端 ID 应该引用始终呈现给客户端的组件。 然后它将恰好是该组件的 HTML 表示,该表示将由 JavaScript 更新。如果该组件的 HTML 表示不是 JSF 呈现给客户端,因为该组件的rendered
是false
,那么 JavaScript 找不到任何东西,并且更新,实际上“什么都没有”会发生。 将其设置为例如@form
表示整个表单:
<h:commandButton id="logOutButton" rendered="#{userIdBean.isLoggedIn}" value="Log Out" type="submit" action="#{loginBean.logoutUser}" > <f:ajax render="@form" /> </h:commandButton>
或一些始终呈现的常见包装器组件:
<h:panelGroup id="group"> <h:outputText id="nodeL" value="Good Luck: #{userIdBean.userId}" style="font-weight:bold" rendered="#{userIdBean.isLoggedIn}" /> <br></br> <h:commandButton value="Submit" action="#{regBean.storeUserId}" /> <h:commandButton id="logOutButton" value="Log Out" action="#{loginBean.logoutUser}" rendered="#{userIdBean.isLoggedIn}"> <f:ajax render="group" /> </h:commandButton> <br></br> <h:outputText value="AJAX Response: #{userIdBean.userId}" rendered="#{userIdBean.isLoggedIn}" /> </h:panelGroup>
使用“ click
”事件而不是“ keyup
”,如下所示。
<f:ajax event="click" render="nodeL logOutButton"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.