Wicket头标记为扩展基页的页面呈现两次

时间:2012-06-28 11:34:41

标签: wicket wicket-1.5 page-layout

我将现有的Web应用程序从Wicket 1.4移植到1.5。在应用程序中有两个模板页面,它们是基页的子项。这些模板被命名为安全且不安全,它们为经过身份验证和未经身份验证的用户定义页面。应用程序中的任何页面都继承自这些模板。在Wicket 1.4中,这个设置没有任何问题。

移植到Wicket 1.5后,我收到以下错误:

  

无法在[HtmlHeaderContainer]中找到ID为'PageTitle'的组件

'PageTitle'是一个Wicket标签,用于在基页中动态构建页面标题,它位于基页标记的<head>标记中。我发现<head>标记正在呈现两次,所以我认为我得到了错误,因为Wicket创建了一次PageTitle然后再次尝试创建它(<head>定义在基页仅标记)。

快速而又脏的修复方法是将PageTitle移动到模板(重复的代码)。有没有更好的方法来解决这个问题?

希望我的描述足够清楚,但是,如果需要,我可以提供代码示例。

2 个答案:

答案 0 :(得分:0)

&lt; head&gt;标签只能在页面标记中使用一次。这意味着如果你有&lt; head&gt;在基页中标记,没有扩展它的页面应该包含它。此外,没有组件应该使用&lt; head&gt;。标签

相反,请使用&lt; wicket:head&gt;标记以包含基页中未包含的任何其他内容。 Wicket将使用&lt; wicket:head&gt;标记以动态地将内容注入&lt; head&gt;呈现并传送到浏览器的标记。

答案 1 :(得分:0)

按照要求,这是一个代码示例。 BasePage.java:

public class BasePage extends WebPage 
{

  public BasePage() 
  {
     this(new PageParameters());
  }

  public BasePage(PageParameters parameters)
  { 
    add(new Label("PageTitle", "Gosh Wicket version migration is hard work"));
  }
  ...

}

BasePage.html(doctype等,已删除):

<html>  
<head>
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
    <title wicket:id="PageTitle">Page title goes here</title>
    <!-- This comment will appears in both the headers I see in the source, therefore this header is rendering twice-->
    <link type="text/css" rel="stylesheet" href="css/application.css" />
    <script type="text/javascript" src="js/rollover.js"></script>
    <script type="text/javascript" src="js/menus.js"></script>
</head>

<wicket:child/>
</html>

UnSecureTemplate java:

public class UnSecureTemplate extends BasePage
{
  public UnSecurePageTemplate()
  {
      super(new PageParameters());
  }


  public UnSecureTemplate(PageParameters parameters) 
  {
    super(parameters);

    Label footerText = new Label("footerText", footerComesFromAPropertiesFile); 
    add(footerText);

    //Instance variables here defined in BasePage       
    // Header bar links - left
    Link<Object> hdrHome = addLink("hdrHome", HomePage.class);//this method is in BasePage in case you're wondering
    hdrHome.add(new Image("mgrLogo", new ContextRelativeResource(poLogoUrl)));

    // Header bar links - Right
    ExternalLink hdrCorporate = new ExternalLink("hdrCorporate", anExternnalLink);
    hdrCorporate.add(new Image("operatorLogo", new ContextRelativeResource(opLogoUrl)));
    add(hdrCorporate);
  }

UnSecureTemplate.html:

<wicket:extend xmlns="http://www.w3.org/1999/xhtml" lang="en"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">


<body id="body" onload="preloadImages(); return true;">

<div class="centerbody">
    <a name="top"></a>
    <div id="mast">
        <a wicket:id="hdrHome" title="home page">
            <img wicket:id="mgrLogo" id="mgr-logo" src="images/logoShort.png" width="171" height="45" wicket:message="alt:remon.logoAltText" /> 
        </a>
        <a wicket:id="hdrCorporate" title="Web Site">
            <img wicket:id="operatorLogo" id="po-logo" src="images/logoNoStrapline.png" height="45" wicket:message="alt:remon.logoAltText" />
        </a>
    </div>

    <div id="mainmenubar" style="width: 100%">
        <div class="menubaritem" style="width: 171px;">
            <a href="#">&nbsp;</a>
        </div>
        <div class="menubaritem" style="width: auto; border-right: none;">
            <a href="#">&nbsp;</a>
        </div>
    </div>

    <div id="mainpanel">
        <div id="leftnav">
            <p>&nbsp;</p>
        </div>

        <div id="rightpanel">

            <wicket:child/>

        </div> <!-- right panel -->
    </div> <!-- main panel -->

    <div id="footer" style="height:15px;">
        <span><a href="#top" style="text-decoration: none;" title="Back to Top"><span wicket:id="footerText">Footer Text</span></a></span>
    </div>
    <div id="footerspacer">
        &nbsp;
    </div>
</div> <!-- centre body -->

</body>
</wicket:extend>

}

应用程序页面,LogIn.java:

public class Login extends UnSecureTemplate 
{


  public Login()  
  {
      this(new PageParameters());
  }

  public Login(PageParameters pageParameters) 
  {
    super(pageParameters);

    String welcomeResourceString = stringObtainedFromPropertiesFile;

    add(new Label("welcome", welcomeResourceString));
    add(new Label("loginHeader", thisAlsoComesFromPropertiesFile);

    LoginForm form = new LoginForm("loginform", new SimpleUser(), pageParameters);
    form.add(new FeedbackPanel("feedback"));
    add(form);
}

...

}

的login.html:

<wicket:extend xmlns="http://www.w3.org/1999/xhtml" lang="en"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">

<h2 wicket:id="welcome">Welcome to the Application</h2>

<div style="margin: 20px 150px 20px 150px; text-align: center;">
    <p wicket:id="loginHeader"></p>
    <form wicket:id="loginform" id="loginform" >
        <table style="display: table; border: 0px; margin: auto;" wicket:message="summary:loginTableSummary">
            <tr style="display: table-row;">
                <td class="login" colspan="2"><span wicket:id="feedback">Feedback</span></td>
            </tr>
            <tr style="display: table-row;">
                <td class="login">
                    <label for="username"><wicket:message key="username">Username</wicket:message></label>
                </td>
                <td class="login">
                    <input wicket:id="username" id="username" type="text" name="user" value="" size="30" maxlength="50"/>
                </td>
            </tr>
            <tr style="display: table-row;">
                <td class="login">
                    <label for="password"><wicket:message key="password">Password</wicket:message></label>
                </td>
                <td class="login">
                    <input wicket:id="password" id="password" type="password" name="pswd" value="" size="30" maxlength="16"/>
                </td>
            </tr>
            <tr style="display: table-row;">
                <td class="login">&nbsp;</td>
                <td class="login"><input class="btn" type="submit" name="Login" value="Login" wicket:message="title:loginButtonTitle"/></td>
            </tr>
        </table>
    </form>
</div>
 </wicket:extend>