Search code examples
javaspring-booticonsvaadinfavicon

Changing FavIcon in Vaadin using pure Java alongside Springboot


I want to change the favicon of the website in Vaadin that is combined with Springboot. I have also opted to go the pure Java route within Vaadin (no html page).

I followed this guide, which explains that the icon I wish to use should be added in the src/main/webapp/icons folder where it will be automatically picked up, resized, etc.

I tried that to no avail, after which I found this thread and subsequently this one. The latter link especially explains that Spring-boot has its own directories and that the webapp folder should be avoided. I tried using the spring directories, but again to no avail.

My resources folder currently looks as such:

Resources directory

My MainView as such:

@Route
@PageTitle("My new title")
public class MainView extends VerticalLayout {

And my SpringBootApplication class as such:

@SpringBootApplication
public class MyVaadinApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MyVaadinApplication.class, args);
    }
}

The icon still remains the default spring leaf:

Browser title bar

Where am I going wrong in trying to set the favicon?


Solution

  • Add icon.png to resources/META-INF/resources/icons/icon.png

    enter image description here

    Then:

    • If you are use progressive web app (@PWA annotation) it should already work
    • If you are use simple vaadin application you should to implements PageConfiguration and add link to your favicon.
    @Route("favicon")
    public class FaviconTest extends Div implements PageConfigurator {
        @Override
        public void configurePage(InitialPageSettings settings) {
            HashMap<String, String> attributes = new HashMap<>();
            attributes.put("rel", "shortcut icon");
            attributes.put("type", "image/png");
            settings.addLink("icons/icon.png", attributes);
        }
    }