Spring 最新版使用Gradle 进行配置,不知道怎样添加Thymeleaf 支持

发布于 2021-11-30 06:24:19 字数 7293 浏览 736 评论 2

从spring 官网下载最新版1.5.1 的init 项目,包管理工具选择Gradle, 下载时就添加JPA, Thymeleaf ,MVC和MySQL 支持。

下载之后,使用resources/application.yml 进行配置,添加了data source 和thymeleaf,然后写了个最简单的controller 如下

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView welcome(ModelMap model) {
        return "index";
    }

我就想加载index.html ,但是无法加载,总是找不到。我就想知道这个文件结构要怎么弄。非常感谢。

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'ChangHong'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    // compile('org.springframework.boot:spring-boot-logging')
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web-services')
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")

    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

运行代码使用

gradle bootRun

下面是application.yml 

# LOGGING
logging:
  config: logback.xml
  file: log/ChangHong.log
 # Log file name. For instance `myapp.log`
  path: log/
 # Location of the log file. For instance `/var/log`

# AUTO-CONFIGURATION
spring:
  autoconfigure:

# THYMELEAF (ThymeleafAutoConfiguration)
  thymeleaf:
    cache: true
 # Enable template caching.
    check-template: true
 # Check that the template exists before rendering it.
    check-template-location: true
 # Check that the templates location exists.
    content-type: text/html
 # Content-Type value.
    enabled: true
 # Enable MVC Thymeleaf view resolution.
    encoding: UTF-8
 # Template encoding.
    excluded-view-names:
 # Comma-separated list of view names that should be excluded from resolution.
    mode: HTML5
 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
    prefix: /
 # Prefix that gets prepended to view names when building a URL.
    suffix: .html
 # Suffix that gets appended to view names when building a URL.
#spring.thymeleaf.template-resolver-order:
 # Order of the template resolver in the chain.
    view-names:
 # Comma-separated list of view names that can be resolved.

# SPRING WEB SERVICES (WebServicesProperties)
  webservices:
    path: /services
 # Path that serves as the base URI for the services.
#spring.webservices.servlet.init:
 # Servlet init parameters to pass to Spring Web Services.
    servlet:
      load-on-startup: -1
 # Load on startup priority of the Spring Web Services servlet.

# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
  datasource:
    continue-on-error: false
 # Do not stop if an error occurs while initializing the database.
#spring.datasource.data:  # Data (DML) script resource references.
#spring.datasource.data-username: root
 # User of the database to execute DML scripts (if different).
#spring.datasource.data-password: bogenmacher2qq
 # Password of the database to execute DML scripts (if different).
    driver-class-name: com.mysql.jdbc.Driver
 # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
    name: ChangHong
 # Name of the datasource.
    username: root
    password: password
 # Login password of the database.
    platform: all
 # Platform to use in the schema resource (schema-${platform}.sql).
    separator: ;
 # Statement separator in SQL initialization scripts.
    sql-script-encoding: UTF-8
 # SQL scripts encoding.
    url: "jdbc:mysql://localhost:3306/ChangHong?useUnicode=true&characterEncoding=UTF-8&useSSL=false"
 # JDBC url of the database.
#spring.datasource.

# SPRING MVC (WebMvcProperties)
  mvc:
    async:
      request-timeout: 0
       # Amount of time (in milliseconds) before asynchronous request handling times out.
    date-format: yyyy-MM-dd
     # Date format to use. For instance `dd/MM/yyyy`.
    dispatch-trace-request: false
     # Dispatch TRACE requests to the FrameworkServlet doService method.
    dispatch-options-request: true
     # Dispatch OPTIONS requests to the FrameworkServlet doService method.
    favicon:
      enabled: true
       # Enable resolution of favicon.ico.
    formcontent:
      putfilter:
        enabled: true
         # Enable Spring's HttpPutFormContentFilter.
    ignore-default-model-on-redirect: true
     # If the content of the "default" model should be ignored during redirect scenarios.
    locale:
     # Locale to use. By default, this locale is overridden by the "Accept-Language" header.
    locale-resolver: accept-header
     # Define how the locale should be resolved.
    log-resolved-exception: false
     # Enable warn logging of exceptions resolved by a "HandlerExceptionResolver".
    #media-types:
     # *:
     # Maps file extensions to media types for content negotiation.
    message-codes-resolver-format:
     # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`.
    servlet.load-on-startup: -1
     # Load on startup priority of the Spring Web Services servlet.
    #static-path-pattern: /resources/
     # Path pattern used for static resources.
    throw-exception-if-no-handler-found: true
     # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request.
    view:
      prefix: /resources/
      suffix: .html

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2021-12-05 09:05:49

我已经补充上了build.gradle 请告诉我怎么添加。这里无法添加zip文件,但是我的代码结构与官方直接下载的init包一模一样。 现在,我以前用maven配置过,但是现在需要用gradle,总是配置不成。请指点。

飘过的浮云 2021-12-04 18:21:15

在build.gradle文件中添加项目依赖即可

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文