【问题标题】:How to build two Wicket applications如何构建两个 Wicket 应用程序
【发布时间】:2012-12-17 12:29:03
【问题描述】:

关于以下链接中的 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 中进行哪些更改?

【问题讨论】:

    标签: java web-applications wicket web.xml


    【解决方案1】:

    您实际上不需要修改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

    【讨论】:

    • 当我将 mountPage() 放入方法 init() 时,它工作正常。我不知道为什么它不起作用,如果 moutPage() 在构造函数中。感谢您的帮助
    • 是的,确实,mountPage 应该在 init() 上,而不是在构造函数上。我已经修改了答案。作为答案的附注,请查看此Wicket in Action entry on page mounting,您可能会发现它很有用。
    【解决方案2】:

    您没有两个应用程序,实际上您有两个页面。 第一个 (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);
    }
    

    【讨论】:

    • 值得一提的是,实际上http://localhost:8080/helloworld/helloworld2 将提供此页面,而不是http://localhost:8080/helloworld2
    • @XaviLópez 除非应用程序安装到根上下文中。 (在概念上讨论这个问题而不明确应用程序的名称,以及页面的名称/它们安装的位置,其中一个可以是应用程序的“索引”,这有点令人困惑。)
    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多