Ciri
Ciri

Reputation: 371

Spring boot unable to resolve html view

I created a Spring Starter Project using eclipse and only checked the Web dependencies checkbox. I wrote a simple html file and a controller, but I always get Whitelabel Error Page. I am not using any template engines, nor do I want to use one at this point. We will later use AngularJS for the frontend, but for the moment I just want to resolve a http page.

Controller:

package demo.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {

    @RequestMapping(value="/")
    public String goHome(){
        return "home";
    }
}

Main class:

package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootTest5Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTest5Application.class, args);
    }
}

home.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

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.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootTest5</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.SpringBootTest5Application</start-class>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Folder structure

Folder structure

Upvotes: 1

Views: 3631

Answers (3)

lateralus
lateralus

Reputation: 1020

Does your spring-context have mapping for resources? In facts the page "home" does not exist, I suppose you have home.jsp in some folder like "jsp". What you need is configuring your spring-context.xml to elaborate the return String of Controller methods to get the right path for the page you want to display. For example:

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

This bean tells Spring to take the Controller output, prefix the folder and make as suffix the file extension to get the correct resource.

Upvotes: 1

Zeronex
Zeronex

Reputation: 434

<body data-ng-cloak>

is not valid XML and Thymeleaf validates the output, you can change Thymeleaf template mode to "Legacy HTML5" or change your view (home.html) to

<body data-ng-cloak="">

Upvotes: 2

Tome
Tome

Reputation: 3364

I guess you are missing a dependency on a templating engine, such as Thymeleaf for instance. Adding this to your POM should be enough:

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

Upvotes: 1

Related Questions