Search code examples
vaadinvaadin7

Serve a static web page within my Vaadin 7 web app?


How do I get my Vaadin 7 app to serve a static page (a .html file) stored within my Vaadin 7 app?

Scenario: I want to occasionally generate a freshened version of a static web page. For example, when a user finished entering data into my Vaadin app, that app generates a report with content written to a report.html file. The initial version of this report is stored in the Web Pages folder of my Vaadin project, alongside the WEB-INF, VAADIN, and META-INF folders.

How can I get the web server to serve that report.html page?

In my case, I am using Tomcat 8 to run my web app both in development and in production. But surely there must be a non-Tomcat-specific way to serve that page.

I know how to open a second browser window using the BrowserWindowOpener class.

Button button = new Button ( "Show Report" );
BrowserWindowOpener browserWindowOpener = new BrowserWindowOpener ( new ExternalResource ( urlStringGoesHere ) );
browserWindowOpener.extend ( button );

While running my Vaadin 7.6.4 app from NetBeans 8.1 in the Safari browser, the URL is http://localhost:8080/AwesomeApp-ui/. So I tried hard-coding that urlStringGoesHere to http://localhost:8080/AwesomeApp-ui/report.html. But the new window displays my Vaadin app’s UI rather than display that static report page.

What is the proper way to store a static page within my Vaadin app and then display upon demand by the user?


Solution

  • Vaadin handles requests at the URLs which you set it up to listen on. Your app seems to be configured to handle all URLs inside it's web context. That means you probably have a servlet-mapping similar to this one:

    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    Note the part where you map your vaadin servlet to all URLs matching the pattern /*.

    If you're using the new Servlet 3.0 API, you're not required to define your servlet mapping in a web.xml file, you might not even have a web.xml at all. In that case search for a subclass of VaadinServlet and look at the annotations of that class, the same metadata can be given through those annotations as in a web.xml. See the Deploying an Application chapter in the Vaadin documentation.

    You might want to change the mapping of the Vaadin Servlet to something like this:

    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/VAADIN/*</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    Now the catch-all URL /* is not assigned to the Vaadin Servlet anymore, so you're free to assign it to another servlet of your choice or let the default servlet serve static resources from your static web resources folder. I recommend creating a servlet if you're planning on serving dynamic resources.