在 WICKET 中显示错误时不添加所有组件

发布于 2024-10-14 00:08:57 字数 190 浏览 2 评论 0原文

我有一个页面需要一个参数。如果这个参数没有通过,那么我想显示一条错误消息(“请传递参数'blah”),该消息将显示在FeedBackPanel中并退出。但是,如果我不附加所有组件,则 wicket 会出现错误,将客户端重定向到 Wicket 的错误页面。

有没有办法显示错误消息而不将所有项目添加到页面?有些项目是 ListViews 等......

I have a page that takes a single argument. If this argument is NOT passed, then I would like to display an error message ("please pass argument 'blah") which will be shown in a FeedBackPanel and bail out. However, if I do not attach all the components, then wicket has an error instead, redirecting the client to Wicket's error page.

Is there any way to display an error messages and NOT add all the items to a page? Some of the items are ListViews, etc...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2024-10-21 00:08:57

如果容器不可见,则不会验证其结构一致性(匹配子级的 id)。因此,只需将您的内容包装在某个容器中,如果未传递参数,则调用 container.setVisible(false) 。然后您可以立即返回,而不添加其子项,Wicket 不会抱怨:

HomePage.java

public class HomePage extends WebPage {
    public HomePage(PageParameters pageParameters) {
        super(pageParameters);
        add(new FeedbackPanel("feedback"));
        WebMarkupContainer container = new WebMarkupContainer("container");
        add(container);
        if (getPageParameters().getString("id") == null) {
            error("Where's my 'id' argument?!?");
            container.setVisible(false);
            //you can return here, Wicket won't complain about not finding the form.
            return;
        }
        Form form = new Form("form");
        form.add(new TextField("field1", new Model()));
        form.add(new TextField("field2", new Model()));
        container.add(form);
    }
}

HomePage.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <ul wicket:id="feedback"></ul>
  <div wicket:id="container">
    <form wicket:id="form">
      <input wicket:id="field1">
      <input wicket:id="field2">
    </form>
  </div>
</body>
</html>

If the a container is not visible, it's not verified for structural consistency (matching childrens' ids). So, just wrap your content in some container and call container.setVisible(false) if the parameter isn't passed. You may then return immediately, without adding its children, Wicket won't complain:

HomePage.java

public class HomePage extends WebPage {
    public HomePage(PageParameters pageParameters) {
        super(pageParameters);
        add(new FeedbackPanel("feedback"));
        WebMarkupContainer container = new WebMarkupContainer("container");
        add(container);
        if (getPageParameters().getString("id") == null) {
            error("Where's my 'id' argument?!?");
            container.setVisible(false);
            //you can return here, Wicket won't complain about not finding the form.
            return;
        }
        Form form = new Form("form");
        form.add(new TextField("field1", new Model()));
        form.add(new TextField("field2", new Model()));
        container.add(form);
    }
}

HomePage.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <ul wicket:id="feedback"></ul>
  <div wicket:id="container">
    <form wicket:id="form">
      <input wicket:id="field1">
      <input wicket:id="field2">
    </form>
  </div>
</body>
</html>
菩提树下叶撕阳。 2024-10-21 00:08:57

最简单的解决方案是完全显示不同的页面。

如果做不到这一点,您可以使用Panel来放入所有可选组件。

像这样:

public MyPage( String param ) {
   if ( param != null ) {
     add( new MyPanel( "content", param ) );
   } else {
     add( new Label( "content", "" );
   }
}

您的html将是

<html>
   <body>
      !-- Feedback panel goes here -->
      ...

      <!-- Content panel goes here -->
      <wicket:container wicket:id="content"/>
   </body>
</html>

您也可以使用`Fragment,但我不这样做不推荐它,它不会给你很好的代码。

The easiest solution is to display a different page altogether.

Failing that, you can use a Panel to put all your optional components in.

Something like this:

public MyPage( String param ) {
   if ( param != null ) {
     add( new MyPanel( "content", param ) );
   } else {
     add( new Label( "content", "" );
   }
}

And your html will be

<html>
   <body>
      !-- Feedback panel goes here -->
      ...

      <!-- Content panel goes here -->
      <wicket:container wicket:id="content"/>
   </body>
</html>

You can also use `Fragments, but I don't recommend it, it doesn't give you very nice code.

情绪操控生活 2024-10-21 00:08:57

我认为托加姆斯勋爵走在正确的道路上。

我偷了这个例子: http://apache-wicket.1842946.n4.nabble.com/Feedback-message-does-not-show-in-new-WebPage-td2993413.html

HTML 片段

Java 片段

public MyPage(boolean failed)
{
    super();

    FeedbackPanel fbpnlFeedbackPanel = new FeedbackPanel("fbpnlFeedbackPanel");
    add(fbpnlFeedbackPanel);

    if (failed)
    {
        // MySession extends WebSession
        MySession ssnSession = (MySession)getSession();
        ssnSession.error("[Error text to put in FeedbackPanel] or use a model");
        ...
    }

    Form<?> frmForm = new Form<?>("frmForm")
    {
        ...
    }
    ...
}

调用 session.error() 的事实向页面添加了足够的内容,以了解 (a) 显示反馈面板和 (b) 使用提供的文本/模型在该面板中。

I think Lord Torgamus is on the right track.

I stole this example: http://apache-wicket.1842946.n4.nabble.com/Feedback-message-does-not-show-in-new-WebPage-td2993413.html

HTML fragment

<div wicket:id="fbpnlFeedbackPanel"/>

Java fragment

public MyPage(boolean failed)
{
    super();

    FeedbackPanel fbpnlFeedbackPanel = new FeedbackPanel("fbpnlFeedbackPanel");
    add(fbpnlFeedbackPanel);

    if (failed)
    {
        // MySession extends WebSession
        MySession ssnSession = (MySession)getSession();
        ssnSession.error("[Error text to put in FeedbackPanel] or use a model");
        ...
    }

    Form<?> frmForm = new Form<?>("frmForm")
    {
        ...
    }
    ...
}

The fact that the session.error() is called adds enough stuff to the page to know to (a) show the feedback panel and (b) use the provided text/model in that panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文