简体   繁体   中英

“No message available” using Spring Boot/Thymeleaf

I'm using Spring Boot with Thymeleaf and I'm trying to access a simple webpage. I don't have any error when building the project. Only this one when accessing localhost:8080/greeting :

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Dec 02 15:36:04 CET 2016
There was an unexpected error (type=Not Found, status=404).
No message available

I have a controller :

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    @RequestMapping("/greeting")
    public String user(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model){
        model.addAttribute("name",name);
        return "greeting";
    }

}

A main:

package boot;

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

@SpringBootApplication
public class Application {

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

}

And a webpage:

<!DOCTYPE html>
<html xmlns:th="http://www.themyleaf.org">
<head>
<meta charset="ISO-8859-1"/> 
<title>SecondMaven</title>
</head>
<body>
    <p th:text="'Hello, '+ ${name} + '!!!!!'" />
</body>
</html>

My pom.xml :

<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>

I don't have any clue of what is wrong with my code. I think that it is a configuration problem, but so far I didn't get any luck. I would appreciate any help, thank you.

Let's take a closer look at your main application entry:

package boot;
^^^^^^^^^^^^^

// imports

@SpringBootApplication
public class Application { ... }

The package boot along with a @SpringBootApplication means that only components in or under boot package will be scanned and registered in the application context. Here is how your controller looks like:

package controller;
^^^^^^^^^^^^^^^^^^^

// imports

@Controller
public class UserController {
    @RequestMapping("/greeting")
    public String user(...) { ... }
}

Since it resides in the controller package, it can't be scanned by the @SpringBootApplication , hence it's not going to be registered and won't handle requests to the /greeting endpoint.

The easiest solution is to move your controllers (and other to be scanned components) under the boot package, eg boot.controller in your particular case.

Anyway, Spring Boot does not require any specific code layout to work, however, there are some best practices that help. You can checkout those best practices here .

Try this:

@Controller
public class UserController {

    @RequestMapping("/greeting")
    public ModelAndView user(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        return new ModelAndView("greeting").addObject("name", name);
    }
}

Here you have an equivalent running example.

I seem to recall having issues with self closing tags in my limited experience with Thymeleaf. Try manually closing the elements, for example

<meta charset="ISO-8859-1"></meta>

instead of

<meta charset="ISO-8859-1"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM