【问题标题】:Integrating HTML and CSS with Kotlin and Spring MVC将 HTML 和 CSS 与 Kotlin 和 Spring MVC 集成
【发布时间】:2017-06-28 03:03:39
【问题描述】:

我想为我的 spring 项目添加一些 html 和 css。我正在运行 gradle,并使用 Kotlin。我当前的树目录是这样的:link(我没有包含 gradle 构建文件)。

我只是想打印“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>

编辑:该网站在本地运行,所以我无法提供链接。 目前,它打印“问候”,这是我在 GreetingController 中返回的内容。但是,我希望它显示为“Hello World”,这是由 html 文件生成的。可能是 html 文件未连接到应用程序的问题。我不确定。任何帮助,将不胜感激。

【问题讨论】:

  • 您的网站链接指向localhost
  • 是的,我的错。忘记了它仅在本地可用。我添加了对出现的内容的描述。它非常简约,所以这不应该是一个问题。

标签: html spring-mvc kotlin


【解决方案1】:

问题是我使用了@RestController,而我应该只使用@Controller。 此外,html 文件应该位于 resources/templates 目录中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多