您实际上不需要修改web.xml 中的任何内容。在那里定义的唯一相关设置是<filter-mapping> 元素
<filter-mapping>
<filter-name>HelloWorldApplication</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
,它将对应用程序(其上下文根)发出的所有请求(/*)映射到 Wicket 过滤器(将其视为一个 servlet),它将处理所有 Wicket 请求并将它们定向到适当的方法(组件构造函数、事件处理方法等)。
在示例中,您在请求http://localhost:8080/helloworld/ 时会看到HelloWorld 页面,因为HelloWorld 是在WebApplication 中定义的主页。 helloworld 是 webapp 的上下文根,因此 Wicket 会自动将您带到 WebApplication#getHomePage() 中定义的页面:
@Override
public Class getHomePage() {
return HelloWorld.class;
}
注意这里的helloworld 是应用程序的上下文根。因此,除非您想在getHomePage() 中定义一些逻辑以根据某些标准返回一个类或另一个类(不要真的认为这是您所追求的),否则它将有效地为HelloWorld 服务。
现在,解决您的问题,使用 Wicket,您可以使用 WebApplication#mountPage() 将页面(可添加书签)挂载到 URL:
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。