Skip to content
Advertisement

What do spring.mvc.view.prefix and spring.mvc.view.suffix have to be?

I created a Spring Boot demo app with Maven using Spring Initializr (that’s my almost the very first usage of Spring). It works, but for some reason doesn’t show any pages besides index.html. If I’m right, that’s because of configuration in application.properties, but I just don’t know, what have I add there.

My project’s sources structure:

src
  main
    java
      irimi
        springbootdemo
          SpringBootDemoApplication.java
          SpringBootDemoApplicationController.java
    resources
      static
        index.html
      templates
        test-form.html
        test-page.jsp
      application.properties

I tried to add different prefixes and suffixes into application.properties, but nothing works. Just as an example:

spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html

Again, index.html opens perfectly.

Maybe, it would be good to show here my dependences too:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

That’s an error page:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 15 11:49:12 MSK 2022
There was an unexpected error (type=Not Found, status=404).
No message available

If my error is really in application.properties, what have I put there?

Advertisement

Answer

With Default Rendering with template

If you are using default “/resources/templates” for rendering view.Spring Boot only includes auto-configuration support for the following templating engines:

  1. FreeMarker
  2. Groovy
  3. Thyme-leaf
  4. Velocity

Example:

Step1:

For using thymeleaf you should add dependency either with gradle and maven Gradle:

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.5.4'

OR Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.5.4</version>
</dependency>

Step2: Add below code with properties file

(Optional)spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

With MVC support

By default, this handler serves static content from any of /static, /public, /resources, and /META-INF/resources directories that are on the classpath. Since src/main/resources is typically on the classpath by default, we can place any of these directories there.

Only static folder is available for rendering view.You can customise using below code with properties file.By default Spring Boot will serve static content from a folder called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.

spring.mvc.static-path-pattern=/content/**
spring.web.resources.static-locations=classpath:/files/,classpath:/static-files

More information visit

https://docs.spring.io/spring-boot/docs/1.1.5.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

https://www.baeldung.com/spring-mvc-static-resources

Advertisement