它是关于以下链接中的 helloworld 示例:
http://wicket.apache.org/learn/examples/helloworld.html
helloworld 工作正常,我可以使用 url 调用应用程序:http://localhost:8080/helloworld/
。现在我想扩展第二个应用程序 hellowolrd2
的示例,这样当我用浏览器调用 http://localhost:8080/helloworld2/
时,第二个页面 helloworld2 出现(类似于 hell 世界)。假设我有文件 HelloWorld2.java
和 HelloWorld2.html
。我应该在文件 web.xml 中更改什么?
最佳答案
您实际上不需要修改 web.xml
中的任何内容.那里定义的唯一相关设置是 <filter-mapping>
元素
<filter-mapping>
<filter-name>HelloWorldApplication</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
,它将对应用程序(其上下文根)发出的所有请求(/*
)映射到 Wicket 过滤器(将其视为 servlet),它将处理所有 Wicket 请求并将它们定向到适当的方法(组件构造函数、事件处理程序方法等)。
在示例中,您会看到 HelloWorld
请求时的页面 http://localhost:8080/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
,你应该部署另一个 webapp,当然有它自己的 web.xml
, 以及上下文根 helloworld2
.
关于java - 如何构建两个 Wicket 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914214/