0

I don't understand how Spring maps view to files (e.g. "Homepage" view to "Home.jsp"). On Websphere you would find it in an XML file. On Spring I see that you map end-points to view on controllers in a Java class, but I cannot actually find how I can choose to map a specific view to a specific file.

Thank you

EDIT 1: It seems like my first problem is that JSP support is not enabled. This is my Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

If I add:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Then I get an error on my pom.xml:

The managed version is 8.5.27 The artifact is managed in org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE

The file reported is org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE.pom and the line report is:

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <version>${tomcat.version}</version>
</dependency>

EDIT 2:

Please consider that, even if I set my spring.mvc.view.suffix: .html, I get error 500 when I try to access http://localhost:8080/greeting. Here it is my console error:

2018-03-11 21:24:40.923 ERROR 4892 --- [nio-8080-exec-5] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-5] Exception processing template "greeting": Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers
2018-03-11 21:24:40.924 ERROR 4892 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers
Giovarco
  • 63
  • 1
  • 11

1 Answers1

0

You need to provide two things to Spring's ViewResolver if you want it to find your view files:
prefix - path to the folder where all searching begins.
suffix - extension of your views.

You can do this via xml:

<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
    </bean>

Via application.properties:

spring.mvc.view.prefix: /WEB-INF/pages/
spring.mvc.view.suffix: .jsp

Via Java Config:

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/pages/");
    resolver.setSuffix(".jsp");
    return resolver;
}

So if you have file /WEB-INF/jsp/home.jsp Spring can find the file only having hello string.

If you have two hello.jsp in different folders it won't be a problem because you need to explicitly tell in what folder your view is reside(for example, you will specify return "/foo/hello" or return "/bar/hello")

Artem Malchenko
  • 2,320
  • 1
  • 18
  • 39
  • Thank you for the kind answer. And it's quite interesting. I'm editing Spring "Serving web content" Example. I don't know what setup does it have (I'm a novice). However, I have no **application.properties**. Does it mean that when I `return "example";` in a Java Controller, the file **example.html** will be fetched? What if there are two **example.html** in different folders? I have one more question: it seems that you have JSP support enabled on Spring. But I wasn't able to add it without breaking my project. Do you have an easy-to-follow guide for me? – Giovarco Mar 11 '18 at 13:36
  • See edit of my answer, I've tried to answer your questions. If you want to enable JSP support in your Spring project see [the link](https://stackoverflow.com/questions/20602010/jsp-file-not-rendering-in-spring-boot-web-application). If I helped you, please consider to mark the answer as "accepted". – Artem Malchenko Mar 11 '18 at 14:19
  • thanks for the answer. Really appreciated. Things start to make sense. So before I can use my **greeting.jsp**, I actually have to enable JSP support. Please see my EDIT 1. – Giovarco Mar 11 '18 at 16:02
  • Your edit is another question and isn't related to this one. Create new question and stop asking multiple questions in one. – Artem Malchenko Mar 11 '18 at 17:24
  • you're right. Please forgive me. I already have an open question about JSP Support on Spring: https://stackoverflow.com/questions/49211178/spring-tool-suite-unable-to-change-my-welcome-page-fron-index-html-to-index-js – Giovarco Mar 11 '18 at 20:20
  • Please consider that, even if I set `my spring.mvc.view.suffix: .html`, I get error 500 when I try to access `http://localhost:8080/greeting`. In EDIT 2 I attach my console error @Artem Malchenko – Giovarco Mar 11 '18 at 20:26