2012-12-17 27 views
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中更改什么?

回答

3

你并不真的需要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

+0

这工作正常,当我把方法init()中的mountPage()。我不知道为什么它不起作用,如果在构造函数moutPage()。感谢您的帮助 –

+0

是的,的确,'mountPage'应该在init()上,而不是在构造函数上。我修改了答案。作为答案的附注,请查看[Wicket in Action entry on page mounting](http://wicketinaction.com/2011/07/wicket-1-5-mounting-pages/),您可能会发现它很有用。 –

1

你没有两个应用程序,实际上你有两个页面。 第一个(的HelloWorld)被作为主页回应映射,它HelloWorldApplication定义:

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

如果你想本地主机:8080/helloworld2 /只是HelloWorldApplication

建立在init()方法的映射
@Override 
public void init() { 
super.init(); 
this.mountPage("/helloworld2", Helloworld2.class); 
} 
+0

这可能是值得一提,实际上通过'http://本地主机:8080 /的HelloWorld/helloworld2'将成为这个页面,而不是通过'http://本地主机:8080/helloworld2'。 –

+0

@XaviLópez除非该应用被安装到根上下文。 (这有点混乱,没有去歧义应用程序的名称概念上讨论这个问题,他们正在安装,其中页/,其中之一可以是“指数”的应用程序的名称。) – millimoose