1ac*_*ac0 3 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("/another.html", StandardPage.class);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我访问/index.html时,我被重定向到/another.html页面.一直以为在创建页面的那一刻,StandardPage.class会被实例化,所以这两个页面将由StandardPage.class的两个独立对象处理?
是的,它是真实的.Wicket有自己复杂的URL处理机制以及如何将浏览器重定向到特定目标.在Wicket中,使用两个或多个不同的路径来安装相同的页面类(默认情况下).
解决方案1
如果您确实希望在不同的URL上获得相同的功能,请使用简单的后代
public class StandardPage2 extends StandardPage {
//... define the same constructors from StandardPage
}
Run Code Online (Sandbox Code Playgroud)
你的代码
@Override
protected void init(){
super.init();
mountPage("/index.html", StandardPage.class);
mountPage("/another.html", StandardPage2.class);
}
Run Code Online (Sandbox Code Playgroud)
别忘了正确使用
setResposonsePage(StandardPage.class);
Run Code Online (Sandbox Code Playgroud)
要么
setResposonsePage(StandardPage2.class);
Run Code Online (Sandbox Code Playgroud)
解决方案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
Run Code Online (Sandbox Code Playgroud)
在这种情况下,可以使用默认的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);
Run Code Online (Sandbox Code Playgroud)
并且您必须使用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
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案3
你也可以覆盖IRequestMapper和其他一些类,但我认为它太复杂了,在你的情况下没有必要.
| 归档时间: |
|
| 查看次数: |
977 次 |
| 最近记录: |