开发者

Not add all components when displaying an error in WICKET

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 argume开发者_C百科nt '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...


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>


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜