2

这是关于以下链接上的 helloworld 示例:

http://wicket.apache.org/learn/examples/helloworld.html

helloworld 工作正常,我可以使用 url: 调用应用程序http://localhost:8080/helloworld/。现在我想扩展第二个应用程序的示例,以便hellowolrd2当我http://localhost:8080/helloworld2/使用浏览器调用时,会出现第二个页面 helloworld2(类似于 helloworld)。假设我有文件HelloWorld2.javaHelloWorld2.html。我应该在文件 web.xml 中更改什么?

4

2 回答 2

3

你真的不需要修改web.xml. 定义的唯一相关设置是<filter-mapping>元素

<filter-mapping>
    <filter-name>HelloWorldApplication</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

,它将/*对应用程序(其上下文根)发出的所有请求( ETC。)。

在示例中,您HelloWorld在请求时会看到该页面,http://localhost:8080/helloworld/因为HelloWorld是在WebApplication. helloworld是 webapp 的上下文根,因此 Wicket 会自动将您带到定义的页面WebApplication#getHomePage()

@Override
public Class getHomePage() {
    return HelloWorld.class;
}

请注意,helloworld这里是应用程序的上下文根。因此,除非您想定义一些逻辑getHomePage()以根据某些标准返回一个类或另一个类(不要真的认为这是您所追求的),否则它将有效地服务于HelloWorld.

现在,解决您的问题,您可以使用 Wicket 将(可添加书签的)页面安装到 URL 的使用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,你应该部署另一个 webapp,当然要使用它自己的web.xml,并使用 context-root helloworld2

于 2012-12-17T15:48:54.320 回答
1

您没有两个应用程序,实际上您有两个页面。第一个 (helloworld) 被映射为响应主页,它在 HelloWorldApplication 中定义:

@Override
public Class getHomePage() {
    return HelloWorld.class;
}

如果你想要 localhost:8080/helloworld2/ 只需在 HelloWorldApplication 的 init() 方法中创建一个映射

@Override
public void init() {
super.init();
this.mountPage("/helloworld2", Helloworld2.class);
}
于 2012-12-17T15:45:48.807 回答