java - 在我的 xhtml 中使用模板 (jsf) 后,commandLink 和 CommandButton 标记不会调用任何操作

标签 java html jsf templates jsf-2

我按以下方式使用 JSF 模板:

TemplateHeader.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>TODO supply a title</title>
    <meta name="viewport" content="width=device-width"/>
</h:head>
     <h:body>

            <ui:composition>
                    <div></div>   
             </ui:composition>

      </h:body>
</html>

模板菜单.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>TODO supply a title</title>
    <meta name="viewport" content="width=device-width"/>
</h:head>
        <h:body>
            <ui:composition>
                <div></div>
            </ui:composition>
        </h:body>
</html>

模板内容.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>TODO supply a title</title>
    <meta name="viewport" content="width=device-width"/>
</h:head>
<h:body>
    <ui:composition>
          <div></div>
    </ui:composition>
</h:body>
</html>    

TemplateFooter.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>TODO supply a title</title>
    <meta name="viewport" content="width=device-width"/>
</h:head>
<h:body>
    <ui:composition>
        <div></div>  
    </ui:composition> 
</h:body>
</html>

然后我将所有这些都包含在一个完整的模板文件中:

完整模板.xhtml

 <html> // with all the necessary tags as above
  <h:head>
    <title>State Transport Department- Work Schedule</title>
    <meta name="viewport" content="width=device-width"/>
    <link  rel="stylesheet" type="text/css" href="../CSS/templateCSS.css"/>
 </h:head>

 <h:body style="with the reqd styles">

 <div style="css styles">

  <div style="">
 <ui:insert name="header">
                <ui:include src="/webpages/templates/TemplateHeader.xhtml"/>
            </ui:insert>

</div>

<div>
 <ui:insert name="menu">
                <ui:include src="/webpages/templates/TemplateMenu.xhtml"/>
            </ui:insert> 
</div>

<div>
  <ui:insert name="content">
                <ui:include src="/webpages/templates/TemplateContent.xhtml"/>
            </ui:insert>
</div>

<div>
 <ui:insert name="footer">
                <ui:include src="/webpages/templates/TemplateFooter.xhtml"/>
            </ui:insert> 
</div>

 </div>

 </h:body>
 </html>

然后我将此页面包含到我的主页中,例如:

测试.xhtml

 <html>
 <h:body>
    <h:form>
<ui:composition template="/webpages/templates/CompleteTemplate.xhtml">

            <ui:define name="menu">
      //override the previos wid menues etc
            </ui:define>

             <ui:define name="content"> 
 <h:commandLink id="allocateButton" value="Test Submit"
 action="#{myTaskBean.viewMyTask}"/> 

 <h:commandButton id="allocateButton" value="Test Submit"
 action="#{empDutySchedBean.testMethod}"/>
              </ui:define>

 </ui:composition>
         </h:form>
      </h:body>
   </html>

所有命令链接和命令按钮都不起作用。用于菜单的普通 html anchor 有效。

对于 CommandLink,我收到的错误为:

This link is disabled as it is not nested within a JSF form.

对于 CommandButton:源代码中的按钮名称不会呈现给内置 jsf,例如 j_something,并且不会调用 java 方法。

有人说这是我做模板的方式有问题。不过我觉得还好。

最佳答案

您的模板制作方式错误,不需要设置 <h:body><h:head>在您的所有页面中。

您的完整结构应类似于:

mainTemplate.xhtml

<!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"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core">
<f:view>
     <h:head>
         <ui:insert name="header">
                 <!-- so that each implementing page can set its own title -->
             <title></title>
         </ui:insert>
    </h:head>

    <h:body>
         <!-- if your menu is shared through all site, otherwise set it as ui:insert -->
         <ui:include src="menu.xhtml" />
         <ui:insert name="body"></ui:insert>
         <ui:insert name="footer"></ui:insert>
    </h:body>
</f:view>

现在您的表单页面将是:

test.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                template="mainTemplate.xhtml">
    <ui:define name="header">
        <title>Test Page</title>
    </ui:define>

    <ui:define name="body">
        <h:form id="myForm">
            <!-- your jsf inputs, commandbutton, etc... -->
        </h:form>
    </ui:define>

    <ui:define name="footer">
        <!-- include can come anywhere inside ui:define -->
        <ui:include src="testPageCustomFooter.xhtml" />
    </ui:define>

</ui:composition>

您当然可以有多个模板,但是您的<ui:composition>可以通过 template 一次实现一个属性。 实现模板并不强制您ui:define所有ui:insert ,这取决于您在每个特定页面中的需要。 另请注意test.xhtml你不要重写<html> , <h:head><h:body> ,您可以直接在 <ui:composition> 内开始实现,以及 <ui:define> 之外的任何内容不会在您的页面中呈现。 希望这能澄清事情。

关于java - 在我的 xhtml 中使用模板 (jsf) 后,commandLink 和 CommandButton 标记不会调用任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22982635/

相关文章:

html - 在容器中居中 ul

jsf - PrimeFaces CSS 皮肤未显示在登录页面中,还有 JavaScript 未定义错误

php - 以两种不同的 div 样式显示数据库中一张表的图像

javascript - 使用SignalR从wcf通知给JS

java - 当使用 JSF1.x 时,EL 可以从 Set<> 读回以查找值吗?

jsf - PrimeFaces 自动完成 ="off"对用户名和密码不起作用

java - Spring Security 4.0.0 + ActiveDirectoryLdapAuthenticationProvider + BadCredentialsException PartialResultException

java - jsp HTTPS POST 请求

java - 如果用户输入某个关键字并显示它,如何使 editText 获取 stringarray 的值?

java - 在Java中,如何重写类似 "typedef double Scalar"的内容?