Sumit Ghosh
Sumit Ghosh

Reputation: 514

Unable to resolve View using Thymeleaf

I am new to Thymeleaf.I am creating a spring-boot app using thymeleaf in view part.

Please find below my pom.xml file[only the dependencies part]:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
 </parent>
 <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>

I have created an application.properties file in following location:src/main/resources

server.port = 8086
spring.mvc.static-path-pattern=/resources/**
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html

Following is my Boot Application Main class:

@SpringBootApplication
@ComponentScan(basePackages="<common package structure>")
public class AdvMain extends SpringBootServletInitializer {

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(AdvMain.class);
    }

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

I have a controller class as follows:

@Controller
public class HomeController {
    @RequestMapping(path = "/info", method = { RequestMethod.GET, RequestMethod.POST })
    public String info(Model model) {
       model.addAttribute("message", "Thymeleaf");
       return "info";
    }

    @RequestMapping(path = "/home", method = RequestMethod.GET)
    public String index() {
       return "index";
    }
 }

When I invoke the following url:

http://localhost:8086/AddViewer/home

I am getting page not found error.

Please find below my tomcat console log:

   Mapped "{[/home],methods=[GET]}" onto public java.lang.String <package_Structure>.HomeController.index()
   Mapped "{[/info],methods=[GET || POST]}" onto public java.lang.String <package_Structure>.HomeController.info(org.springframework.ui.Model)
   Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
   Tomcat started on port(s): 8086 (http)
   FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms
   No mapping found for HTTP request with URI [/AddViewer/home] in DispatcherServlet with name 'dispatcherServlet'

Can anyone provide any suitable solution to this???

Upvotes: 0

Views: 1602

Answers (2)

Min Hyoung Hong
Min Hyoung Hong

Reputation: 1202

This is not a problem of thymeleaf.
In your console message, you have two mappings, /home and /info.

Mapped "{[/home],methods=[GET]}" onto public java.lang.String <package_Structure>.HomeController.index()
Mapped "{[/info],methods=[GET || POST]}" onto public java.lang.String <package_Structure>.HomeController.info(org.springframework.ui.Model)

But you made a request /AddViewer/home. So, it does not matches with request mappings.

Request like this.

http://localhost:8086/home

Upvotes: 1

ovi
ovi

Reputation: 30

if you using thymeleaf put your view files in src/main/resources/template and no need to add these two line -

spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html

Upvotes: 0

Related Questions