2017-06-27 61 views
0

我想爲我的spring項目添加一些html和css。我正在運行gradle,並使用Kotlin。我目前的樹目錄是這樣的:link(我沒有包含gradle構建文件)。集成HTML和CSS與Kotlin和Spring MVC

我只是試圖打印「Hello $ name $」給出一些輸入的網址。這工作。這裏是GreetingController.kt:

@RestController 
    class GreetingController { 

    @RequestMapping("/greeting") 
     fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String, model: Model) { 
     model.addAttribute("name", name); 
     return "greeting"; 
    } 
} 

我gradle.build文件:

buildscript { 
ext.kotlin_version = '1.1.2' // Required for Kotlin integration 
ext.spring_boot_version = '1.5.3.RELEASE' 
repositories { 
    jcenter() 
    mavenCentral() 
} 
    dependencies { 
     classpath "se.transmode.gradle:gradle-docker:1.2" 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration 
     classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version" 
    } 
} 

apply plugin: 'docker' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'kotlin' // Required for Kotlin integration 
apply plugin: 'org.springframework.boot' 
apply plugin: 'application' 

jar { 
    baseName = 'webapp' 
    version = '0.1.0' 
} 

repositories { 
    jcenter() 
} 

dependencies { 
    compile "org.springframework.boot:spring-boot-starter-thymeleaf" 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration 
    compile 'org.springframework.boot:spring-boot-starter-web' 
    testCompile 'junit:junit' 
} 

task buildDocker(type: Docker, dependsOn: build) { 
    push = false 
    applicationName = jar.baseName 
    dockerfile = file('src/main/docker/Dockerfile') 
    doFirst { 
     copy { 
      from jar 
      into stageDir 
     } 
    } 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '3.5' 
} 

springBoot { 
    mainClass = 'com.tunnll.spring.webapp.Application' 
} 

此外,我的HTML文件:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
<title>Getting Started: Serving Web Content</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 
    <p th:text="'Hello, ' + ${name} + '!'" /> 
</body> 
</html> 

編輯:該網站是在本地運行,所以我不能提供一個鏈接。 目前,它打印出「Greeting」,這是我在GreetingController中返回的內容。但是,我希望它顯示爲「Hello World」,這是由html文件生成的。這可能是html文件沒有連接到應用程序的問題。我不確定。任何幫助,將不勝感激。

+0

您的網站鏈接指向'localhost'。 – zsmb13

+0

是的,我的不好。已經忘記它只在當地可用。我添加了對出現內容的描述。它非常簡約,所以它不應該成爲這方面的問題。 –

回答

0

問題是,當我只使用@Controller時,我正在使用@RestController。 此外,html文件應該位於resources/templates目錄中。