3

I have Wicket app, in WebApplication I do:

public class AppStart extends WebApplication{

    public AppStart(){
    }

    @Override
    protected void init(){
        super.init();
        mountPage("/index.html", StandardPage.class);
        mountPage("/another.html", StandardPage.class);
    }
}

but when I access /index.html then I'm redirected to /another.html page. Has been thinking that at the moment of creating page StandardPage.class will be instantiated so these two pages will be handled by two separate objects of StandardPage.class?

1ac0
  • 2,875
  • 3
  • 33
  • 47

1 Answers1

5

yes, it is true. Wicket has its own complex mechanism of handling URLs and how to redirect browser to a specific target. In Wicket using two or more different paths to mount the same page class has no idea (by default).

SOLUTION 1

If you really want to get the same functionality on different URLs, use a simple descendant

public class StandardPage2 extends StandardPage {
    //... define the same constructors from StandardPage
}

Your code

@Override
protected void init(){
    super.init();
    mountPage("/index.html", StandardPage.class);
    mountPage("/another.html", StandardPage2.class);
}

Do not forget to use correctly

setResposonsePage(StandardPage.class); 

or

setResposonsePage(StandardPage2.class);

SOLUTION 2

The following example shows how you can use a page parameter as a part of URL. Let's there are users in a databases and each user has its own page that is accessible on a unique URL. Also each user can do some optional operations and the operation name is ever included in URL and there are some other pages mounted to their own URIs. So URIs should look like

/home 
/login 
/logout 
/martin 
/martin/edit 
/martin/detail 
/petr
/petr/edit 
/petr/detail
/
/texts/my-super-article-1
/texts/my-super-article-2
/events/actual
/fotos/from-my-first-holiday
/fotos/from-my-second-holiday

In this case it is possible to use the default MontedMapper, that is ever implemented in Wicket. The mapping is

mountPage("/home", HomePage.class);
mountPage("/login", LoginPage.class);
mountPage("/logout", LogoutPage.class);
mountPage("/${nick}/${action}", UserProfilePage.class);
mountPage("/texts/${page}", TextPages.class);
mountPage("/events/${page}", EventPages.class);
mountPage("/fotos/${page}", FotoPages.class);

and you have to implement the UserProfilePage and its constructor with PageParameters

public class UserProfilePage extends WebPage {

     public UserProfilePage(PageParameters pageParameters) {
         super(pageParameters);
         StringValue nick = pageParameters.get("nick");
         StringValue action = pageParameters.get("action");
         // any code
         String nickName = nick.toString();
         boolean defaultAction = action.isEmpty(); // default action
     }

}

SOLUTION 3

Also you can override IRequestMapper and some other classes, but I think it is too much complicated and it is not necessary in your case.

1ac0
  • 2,875
  • 3
  • 33
  • 47
Martin Strejc
  • 4,307
  • 2
  • 23
  • 38
  • Martin, thanks. My situation is quite specific: I don't know forward how many mount points will be handled by the same page class. My code reads database and mount diferent records data (specific record hold mount point) into same page class. One more solution I now studying is Javassist - creating page class as subclass of StandardPage on-the-fly and mount them on specific path. At the moment not know if it's possible. – 1ac0 Dec 25 '13 at 20:27
  • 1
    @LadislavDANKO that a special case. The explanation is a bit loner, but see http://www.wicket-library.com/wicket-examples-6.0.x/mappers/en_US A custom mapper is probably a better solution than JavaSsist on the fly code creating. – Martin Strejc Dec 25 '13 at 20:40