【问题标题】:Spring boot with thymeleaf doesn't work带有百里香叶的弹簧靴不起作用
【发布时间】:2015-10-10 18:39:18
【问题描述】:

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>

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

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


    </dependencies>

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

</project>

应用

@SpringBootApplication
public class Application {


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

控制器

@Controller
@RequestMapping("/index")
public class IndexController {

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


}

我在资源文件夹和 error.html 和 index.html 中有模板文件夹

当我访问 localhost:8080/index 时,会显示错误而不是索引。我究竟做错了什么?这确实是最简单的设置,但它已经错了......

【问题讨论】:

  • 你有application.properties文件吗?
  • 我没有,没必要
  • 您应该访问 localhost:8080/index/ (带有斜杠),因为您请求了它。或者去掉方法级映射@RequestMapping("/")。

标签: spring spring-boot thymeleaf


【解决方案1】:

问题出在你的IndexController 班级。在您声明它的方式中,您可以通过:http://localhost:8080/index/ 而不是localhost:8080/index url 访问您的页面。这就是为什么你在你的方法上有类注释@RequestMapping 和相同的注释。

url 构造是某种层次结构 - 首先是 spring 检查类注释,然后是方法注释和构建的请求映射处理程序是:host + port + "index" + "/"

【讨论】:

  • 你是对的。但是当我第一次阅读它时,我以为你的意思是它缺少“http://”。
猜你喜欢
  • 2021-12-11
  • 2018-04-02
  • 1970-01-01
  • 2016-06-01
  • 2018-11-20
  • 2018-10-12
  • 2014-05-13
  • 1970-01-01
相关资源
最近更新 更多