Java Apache Wicket中一个类的多个路径

Java Apache Wicket中一个类的多个路径,java,wicket,wicket-1.5,Java,Wicket,Wicket 1.5,我有Wicket应用程序,在WebApplication中我有: public class AppStart extends WebApplication{ public AppStart(){ } @Override protected void init(){ super.init(); mountPage("/index.html", StandardPage.class); mountPage("/ano

我有Wicket应用程序,在WebApplication中我有:

public class AppStart extends WebApplication{

    public AppStart(){
    }

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

但是当我访问/index.html时,我会被重定向到/other.html页面。一直在想,在创建页面StandardPage.class时,将实例化这两个页面,以便由StandardPage.class的两个独立对象处理这两个页面?

是的,确实如此。Wicket有自己复杂的机制来处理URL以及如何将浏览器重定向到特定目标。在Wicket中,使用两个或多个不同的路径装载同一个页面类是不知道的(默认情况下)

解决方案1

如果您真的想在不同的URL上获得相同的功能,请使用简单的子代

public class StandardPage2 extends StandardPage {
    //... define the same constructors from StandardPage
}
您的代码

@Override
protected void init(){
    super.init();
    mountPage("/index.html", StandardPage.class);
    mountPage("/another.html", StandardPage2.class);
}
别忘了正确使用

setResposonsePage(StandardPage.class); 

解决方案2

以下示例显示如何将页面参数用作URL的一部分。 假设数据库中有用户,每个用户都有自己的页面,可以通过唯一的URL访问该页面。 此外,每个用户都可以执行一些可选操作,操作名称始终包含在URL中,并且还有一些其他页面装载到他们自己的URI中。 所以URI应该看起来像

/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
在这种情况下,可以使用默认的MontedMapper,它在Wicket中实现过。 映射是

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);
您必须使用PageParameters实现UserProfilePage及其构造函数

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
     }

}
解决方案3


您也可以覆盖IRequestMapper和其他一些类,但我认为这太复杂了,在您的情况下不需要它。

Martin,谢谢。我的情况非常具体:我不知道同一个页面类将处理多少装载点。我的代码读取数据库并将不同的记录数据(特定记录保持装载点)装载到同一个页面类中。我现在研究的另一个解决方案是Javassist——动态创建页面类作为StandardPage的子类,并将它们装载到特定路径上。目前还不知道这是否可能。@LadislavDANKO这是一个特例。这个解释有点孤立,但是请看,定制映射器可能是比JavaSsist动态代码创建更好的解决方案。
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
     }

}