【问题标题】:Wicket head tag rendered twice for pages extending a base page为扩展基本页面的页面呈现两次检票头标记
【发布时间】:2012-06-28 11:34:40
【问题描述】:

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

移植到 Wicket 1.5 后出现以下错误:

在 [HtmlHeaderContainer] 中找不到 ID 为“PageTitle”的组件

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

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

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

【问题讨论】:

  • 你能显示一些代码吗?我们使用相同的技术,而且效果很好
  • 查看下面的代码...顺便问一下,您使用的是哪个版本的 Wicket?

标签: wicket wicket-1.5 page-layout


【解决方案1】:

标签只能在页面标记中使用一次。这意味着如果您在基本页面中有 标记,则扩展它的任何页面都不应包含它。此外,任何组件都不应使用 标签。

改为使用 标记来包含未包含在基本页面中的任何其他内容。 Wicket 将使用 标签将内容动态注入到呈现并传递给浏览器的 标签中。

【讨论】:

  • 好的,谢谢,我认为我们正在这样做(除了 我需要审查...如果可以避免的话,我认为最好避免在头部添加东西通过java代码)
  • 最终这一切都是通过 wicket 中的 javacode 添加的,因为 java 正在逐步解析您的 html 文件,并呈现将返回给浏览器的响应。对于 和 标签,它们会生成一个 HtmlHeaderResolver (ci.apache.org/projects/wicket/apidocs/1.5.x/org/apache/wicket/…) 组件,该组件管理将内容合并在一起以呈现到一个 标签。
【解决方案2】:

按照要求,这是一个代码示例。 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);
}

...

}

登录.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>

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2016-03-27
    • 2013-08-18
    • 2014-02-09
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多