Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring控制器路由中呈现html文件?_Java_Spring_Spring Boot_Intellij Idea_Flask - Fatal编程技术网

Java 如何在Spring控制器路由中呈现html文件?

Java 如何在Spring控制器路由中呈现html文件?,java,spring,spring-boot,intellij-idea,flask,Java,Spring,Spring Boot,Intellij Idea,Flask,我正在使用IntelliJ、Maven和Spring构建本地web服务器。当我返回Spring的@Controller/resources/static/jimi.html的位置时,Java服务器只是将字符串打印到屏幕上,而不是呈现html文件 <html> <head><head> <body> "You are here" </body> </html> 我不熟悉spring框架。我按照start.spring

我正在使用IntelliJ、Maven和Spring构建本地web服务器。当我返回Spring的@Controller/resources/static/jimi.html的位置时,Java服务器只是将字符串打印到屏幕上,而不是呈现html文件

<html>
<head><head>
<body>
    "You are here"
</body>
</html>
我不熟悉spring框架。我按照start.spring.io教程设置了一个java web应用程序。我目前已使用IntelliJ(默认)设置并运行了基础。我的问题如下:我只能找出如何成功地将类型字符串返回到localhost:8080/jimi

我通常用Python编写代码,我知道Spring框架有@Controller包装器,类似于Python中的Flask@app.route包装器。我想返回一个html文件而不是字符串,但不知道如何返回。我编写了以下内容,成功地在本地maven服务器上提供字符串内容:

@Controller
public class ShelfController {

    @GetMapping("/")
    @ResponseBody
    public String home(){
        return "You are here";
    }

    @GetMapping("/jimi")
    @ResponseBody
    public String jimi(){
        //STUCK HERE
    }
}
第一个路由“localhost:8080/”工作正常,并将“Your are here”字符串打印到屏幕上。当我打开inspector时,我看到它正在将字符串返回到这个自动生成的html文件的主体

<html>
<head><head>
<body>
    "You are here"
</body>
</html>
我正在寻找一些类似于Python Flask的代码行(JavaSpring)

@app.route("/jimi")
def jimi():
    return render_template("jimi.html")

它将在给定的路径127.0.0.1:8080/jimi返回jimi.html模板。答案是:通过实现thymeleaf

1) 将jimi.html移动到src/main/resources/templates存储库

2) 函数应返回字符串“jimi”(不带“.html”)

3) 从“/jimi”路径中删除@ResponseBody注释

4) jimi.html需要

5) 将thymeleaf-spring依赖项添加到pom.xml

//SHELFCONTROLLER.JAVA CODE
@Controller
public class ShelfController {

    @GetMapping("/")
    @ResponseBody
    public String home(){
        return "You are here";
    }


    @GetMapping("/jimi")
    public String jimi(){
        return "jimi";
    }
}

//JIMI.HTML CODE
<!-- HTML CODE -->
<html xmlns:th="http:www.thymeleaf.org" lang="en">
  <head></head>
  <body>Hendirx is a Voodoo Chile</body>
</html>

//ADD TO POM.XML
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
//SHELFCONTROLLER.JAVA代码
@控制器
公共类搁置控制器{
@GetMapping(“/”)
@应答器
公共字符串home(){
返回“您在这里”;
}
@GetMapping(“/jimi”)
公共字符串jimi(){
返回“吉米”;
}
}
//JIMI.HTML代码
亨迪克斯是智利的巫毒
//添加到POM.XML
org.springframework.boot
弹簧启动装置