使用Spring Boot和Thymeleaf

时间:2018-09-05 00:03:20

标签: spring-boot thymeleaf

我正在用Thymeleaf运行一个简单的Spring Boot 2.0.4 MVC应用程序。我有2个控制器。第一个是IndexController.java,它将URL“ localhost:8080 /”映射到模板index.html。第二个产品ProductController.java将URL“ localhost:8080 / product”映射到模板product.html。在index.html模板上,我有一个指向产品模板的链接:

     <a href="product.html"> 

当我运行项目并将鼠标悬停在浏览器中的此链接上时,显示的URL为“ localhost:8080 / product.html”。如果单击链接,则会出现404错误。如果我在浏览器的URL框中手动输入“ localhost:8080 / product”,则产品页面将正确显示。因此,我无法使用URL“ localhost:8080 / product.html”。我以为使用Spring Boot和Thymeleaf,任何扩展名“ localhost:8080 / product [。*]”都可以直接使用。

我使用内置的Spring Boot配置器通过IntelliJ 2018创建了这个项目。我只有一个配置文件SfgThymeleafCourseApplication.java:

package guru.springframework;

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

@SpringBootApplication
public class SfgThymeleafCourseApplication {

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

我的索引控制器是IndexController.java:

package guru.springframework.controllers;

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

@Controller
public class IndexController {

    @RequestMapping("/")
    public String getIndex() {
       return "index";
    }

}

我的产品控制器ProductController.java:

package guru.springframework.controllers;

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

@Controller
public class ProductController {

   @RequestMapping("/product")
   public String getProduct() {
     return "product";
   }
}

我的POM看起来像

<?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>guru.springframework</groupId>
<artifactId>sfg-thymeleaf-course</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sfg-thymeleaf-course</name>
<description>Thymeleaf Course</description>

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <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>

0 个答案:

没有答案