Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-6461

Default constructor is incorrectly called if optional param is not provided in parameter placeholder URL with additional required parameter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 6.22.0
    • 8.0.0-M8, 7.10.0
    • wicket
    • None

    Description

      From the documentation here:

      https://ci.apache.org/projects/wicket/guide/6.x/guide/urls.html

      I'm following the section titled, "Using parameter placeholders with mounted pages".

      In the documentation from here:

      https://cwiki.apache.org/confluence/display/WICKET/Request+mapping

      Named parameters - /page/${named1}/${named2}
      
      mountPage("/page/${named1}/${named2}", MyPage.class);
      Now a request to "/page/a/b" will be handled by MyPage and the parameters can be get with PageParameters.get(String) (e.g. parameters.get("named1") will return "a")
      
      Optional named parameters - /page/${named1}/#{named2\
      
      mountPage("/page/${named1}/#{named2}", MyPage.class);
      This means the second parameter is optional. Requests to "/page/a/b" , "/page/a/b/" and "/page/a/" will be handled by MyPage and the parameters can be get with PageParameters.get(String) (e.g. parameters.get("named2") will return "b" for the first case and null for the second).
      The mapper is smart enough to handle optional named parameters in any segment, not just the last one.
      

      it states: "The mapper is smart enough to handle optional named parameters in any segment, not just the last one."

      I've found it does matter IF other segments in the url are also parameter placeholders. The resulting behaviour is that if the optional parameter placeholder isn't provided in the URL, the page's default constructor gets called instead of the PageParameter constructor as expected (since there are still additional PageParameter data that may need to be processed by the page).

      Here is my example:

      From child class of AdtAuthenticatedWebApplication

      @Override
      protected void init() 
      {
          super.init();
      
          mountPage("/foo/#{optParam}/${otherParam}", MyPage.class);
          //mountPage("/foo/${otherParam}/#{optParam}",  MyPage.class); <--- NOTE: This works as expected
      }
      

      In child class of WebPage

      public class MyPage extends WebPage
      {
          public MyPage(PageParameters parameters) 
          {
              super(parameters);
      
              StringValue optParam = parameters.get("optParam");
              StringValue otherParam = parameters.get("otherParam");
      
             //do something...
          }
      
          private MyPage()
          {        
              throw new IllegalAccessError("The default constructor should not be instantiated");
          }
      

      And in a different class:

      public static BookmarkablePageLink getBookmarkableLink(String id, String optParam, String otherParam)
       {         
              PageParameters pageParam = new PageParameters();
                      
              pageParam.add("otherParam", otherParam);
      
              if (optParam != null && !optParam.isEmpty())
              {
                  pageParam.add("optParam", optParam);
              }
                      
              return new BookmarkablePageLink(id, MyPage.class, pageParam);             
          } 
      

      Here are the result of clicking on the BookmarkablePageLinks created:

      /foo/baz/bar  -- calls MyPage(PageParameters parameters) constructor
      /foo/bar -- attempts to call MyPage default constructor (exception thrown)
      

      Exception thrown is:

      Last cause: Class org.apache.wicket.session.DefaultPageFactory can not access a member of class ca.example.for.testing.MyPage with modifiers "private"
      WicketMessage: Can't instantiate page using constructor 'private ca.example.for.testing.MyPage()'. This constructor is private!
      

      Attachments

        1. quickstartPP.zip
          5.54 MB
          Melissa Beldman

        Activity

          People

            svenmeier Sven Meier
            mweston4 Melissa Beldman
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: