When it comes to web application navigation, I’m sure at one point you would like to set up a home page redirect.
If you’re looking for a practical example using Spring Boot, then you’ll love this code sample.
So here we go.
If you want to learn more about PrimeFaces for JSF – head on over to the JSF PrimeFaces tutorials page.
1. What is a Welcome-File-List?
A welcome-file-list allows you to specify a list of files that the web container will use for appending to a request for a URL that is not mapped to a web component.
The following example shows how to set a welcome file redirect for your PrimeFaces JSF web application using Spring Boot and Maven.
2. General Project Overview
We will use the following tools/frameworks:
PrimeFaces 6.2
JoinFaces 3.3
Spring Boot 2.1
Maven 3.5
Our project has the following directory structure:
3. Changing the Default Welcome Page for Spring Boot
We start from a previous JSF Spring Boot Tutorial in which we created a greeting dialog using a first and last name input form.
As we are running on Spring Boot we no longer have a web.xml in which we can specify a <welcome-file-list>.
Create a WelcomePageRedirect class that implements WebMvcConfigurer.
Annotate it with @Configuration. This indicates that the class can be used by the Spring IoC container as a source of bean definitions. In other words, we can specify the page redirect using Java-configuration instead of XML.
Override the addViewControllers() method and forward the default mapping to the target web page. In this example, we will forward to helloworld.xhtml as shown below.
To test the above configuration, open a command prompt. Execute following Maven command to start the JSF Hello World web application.
The resulting console log should mention ‘Started SpringPrimeFacesApplication’ which indicates our web application is up and running.
Open a web browser and enter the following URL: http://localhost:8080/. The below web page should now be displayed.
This means that the redirect was successfully applied as otherwise a ‘Whitelabel Error Page’ error would have been returned as shown below.
If you would like to run the above code sample you can get the full source code here.
This concludes the short code sample on how to set up a JSF welcome file XHTML using Spring Boot.