1

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

3 Answers 3

2
<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="">
Sign up to request clarification or add additional context in comments.

Comments

1

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>

4 Comments

That did work, however, I do not wish to use any templating engine. The index.html file that we will eventually use contains the following line (AngularJS tag): <body data-ng-cloak> This throws an org.xml.sax.SAXParseException saying that data-ng-cloak must be followed by an "=" sign.
I think that without a templating engine, return "home"; will never work, you will have to handle the access to your homepage without using a Controller
Tome, can you explain why you must have a templating engine to return an html file ("home")? This answer would be very helpful if you add some information to support that assertion.
The OP has placed the home.html file in a templates directory, which is the usual place for files that have to be processed by a template engine (like Thymeleaf thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html). If you just have to serve static content, Spring Boot autoconfigures a ResourceHandler so that placing something in the staticdirectory will be served directly.
1

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.

2 Comments

Spring Boot configures an InternalResourceViewResolver that can be configured via spring.view.prefix and spring.view.suffix. Unfortunately, it didn't work for me. Adding a templating engine did the trick, but then I was back to my original problem where the templating engine didn't really understand AngularJS tags. I accepted the answer of Zeronex, apparently the solution was much simpler than trying to use no templating engine at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.