简体   繁体   English

<f:ajax event>不配合<ui:composition template=“”>

[英]<f:ajax event> not working with <ui:composition template=“”>

i have a weird problem and i can't know the main cause, i have these login page, which uses <f:ajax event=""> to validate username and password: 我有一个奇怪的问题,我不知道主要原因,我有这些登录页面,该页面使用<f:ajax event="">来验证用户名和密码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head></h:head>
<h:body>
<h:form>
<h:panelGrid columns="3">
Name:
<h:inputText id="name" value="#{validateBean.name}"
    validator="#{validateBean.validateName}">
    <f:ajax event="blur" render="nameError" />
</h:inputText>
            <h:message for="name" id="nameError" style="color:red" />

Password:
<h:inputText id="password" value="#{validateBean.password}"
    validator="#{validateBean.validatePassword}">
    <f:ajax event="blur" render="passwordError" />
</h:inputText>
        <h:message for="password" id="passwordError" style="color:red" />
                <h:commandButton value="Login" action="# {validateBean.login}" />

</h:panelGrid>

these JSF works properly, but the problem appears when i place the same above components under a <ui:composition template=""> inorder to use these page with a template 这些JSF可以正常工作,但是当我将上述相同组件放在<ui:composition template="">以便将这些页面与模板一起使用时,就会出现问题

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="/WEB-INF/Templates/BasicTemplate.xhtml">
<ui:define name="content">
    <center>
        <h:form>
            <h:panelGrid columns="3">
Name:
<h:inputText id="name" value="#{validateBean.name}"
                    validator="#{validateBean.validateName}">
                    <f:ajax event="blur" render="nameError" />
                </h:inputText>
                <h:message for="name" id="nameError"  style="color:red" />
Password:
<h:inputText id="password" value="#{validateBean.password}"
                    validator="# {validateBean.validatePassword}">
                    <f:ajax event="blur" render="passwordError"  />
                </h:inputText>
                <h:message for="password" id="passwordError"   style="color:red" />
                <h:commandButton value="Login" action="# {validateBean.login}" />
            </h:panelGrid>
        </h:form>
    </center>
</ui:define>
</ui:composition>
</html>

for more clarity, here's the jsf template page: 为了更加清晰,这是jsf模板页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<title><ui:insert name="title">Default title</ui:insert></title>
</head>

<body>

<div id="header">
<ui:insert name="header">

    <ui:include src="header.xhtml"/> // normal page contains plain text
 </ui:insert>
 </div>

 <div id="content">
 <ui:insert name="content">
    Content area.  See comments below this line in the source.

  </ui:insert>
  </div>

   <div id="footer">
   <ui:insert name="footer">

    <ui:include src="footer.xhtml"/> // normal page contains plain text
    </ui:insert> 
    </div>

    </body>

     </html>

when i tried the later approach the AJAX action doesn't fire at all, please any one explain to me why that happens?? 当我尝试后一种方法时,AJAX动作根本不触发,请任何人向我解释为什么会发生这种情况?

You used <head> instead of <h:head> in your master template. 您在主模板中使用了<head>而不是<h:head> This way JSF is unable to auto-include the required jsf.js JavaScript file for the ajax works. 这样,JSF无法自动包含ajax工作所需的jsf.js JavaScript文件。

Fix it accordingly: 相应地修复它:

<html ...>
    <h:head>
        ...
    </h:head>
    <h:body>
        ...
    </h:body>
</html>

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM