你并不真的需要web.xml
修改任何内容。只有相关的设置定义存在<filter-mapping>
元素
<filter-mapping>
<filter-name>HelloWorldApplication</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
,它映射到检票过滤器(/*
)到应用程序发出的所有请求(上下文根)(认为它作为一个servlet),将处理所有Wicket请求并引导它们到合适的方法(组件构造函数,事件处理方法等)。
在此示例中,您在请求http://localhost:8080/helloworld/
时看到HelloWorld
页面,因为HelloWorld
是在WebApplication
中定义的主页。 helloworld
对于Web应用程序上下文根,因此自动检票带您在WebApplication#getHomePage()
定义的页面:
@Override
public Class getHomePage() {
return HelloWorld.class;
}
注意helloworld
这里是应用程序的上下文根。所以,除非你想在getHomePage()
中定义一些逻辑来根据某些标准返回一个类或另一个类(不真的认为这是你所追求的),否则它将有效地服务于HelloWorld
。现在
,解决你的问题,与检票您使用WebApplication#mountPage()
可以挂载(可标记)页面,网址:
public class HelloWorldApplication extends WebApplication {
@Override
protected void init() {
mountPage("/helloworld", HelloWorld.class);
mountPage("/helloworld2", HelloWorld2.class);
}
@Override
public Class getHomePage() {
return HelloWorld.class;
}
}
这将使http://localhost:8080/helloworld/
服务HelloWorld
类,是主页。但也会为它提供请求http://localhost:8080/helloworld/helloworld
。要求http://localhost:8080/helloworld/helloworld2
将有效地服务HelloWorld2
。
或者,如果你真的想http://localhost:8080/helloworld2/
服务HelloWorld2
,你应该部署Web应用程序的另一个当然的,有自己的web.xml
,并与上下文根helloworld2
。
这工作正常,当我把方法init()中的mountPage()。我不知道为什么它不起作用,如果在构造函数moutPage()。感谢您的帮助 –
是的,的确,'mountPage'应该在init()上,而不是在构造函数上。我修改了答案。作为答案的附注,请查看[Wicket in Action entry on page mounting](http://wicketinaction.com/2011/07/wicket-1-5-mounting-pages/),您可能会发现它很有用。 –