继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring Boot 基础框架 - Spring MVC + Thymleaf 环境搭建、配置

慕姐8265434
关注TA
已关注
手记 1267
粉丝 222
获赞 1065

搭建基本环境

目前Spring 官方提供了web的方式进行快速构建,可访问地址:https://start.spring.io/ 地址,那这里为了手把手教大家,就自己动手、丰衣足食了,有兴趣的小伙伴可以自己动动小手指,自己DIY

使用Eclipse 创建maven项目
项目名称:spring-boot-infrastructure-projects

打开eclipse , File > New > Maven Project


webp

New Maven project

选择Next >, 选择Artifact Id : maven-archetype-quickstart

webp

maven-archetype-quickstart


选择Next >, 输入项目信息

webp

2018-9-1 12-46-33.png


选择Finish完成

webp

spring-boot-infrastructure-projects


创建源文件:
src/main/resource
src/test/resource

Eclipse > 文件 > 新建 > 新建 Source Folder


webp

src/mian/resources

至此,一个简单的Maven项目构建完成,截图如下


webp

Maven Project Build Finished

配置Spring Boot + Spring MVC  + Thymleaf Hello World 环境

pom.xml文件

<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>com.lab</groupId>
    <artifactId>spring-boot-infrastructure-projects</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-infrastructure-projects</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    
        <!-- 
            | Spring Boot
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- 
            | thymeleaf
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>
        
    </dependencies>
    
    <!-- 
        | Spring IO Platform 依赖管理器,将Spring各个组件之间的版本兼容性交给Spring来统一维护管理,避免存在兼容性问题。
     -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Cairo-RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    
    <build>
        <finalName>spring-boot-infrastructure-projects</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build></project>

配置文件: application.yml

server:
  port: 8080
  servlet:
    context-path: /
spring:
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true
  messages:
    encoding: UTF-8
  thymeleaf:
server:
  port: 8080
  servlet:
    context-path: /
spring:
  http:
    encoding:
      charset: UTF-8
      force: true
      enabled: true
  messages:
    encoding: UTF-8
  thymeleaf:    # Prefix that gets prepended to view names when building a URL.
    prefix: classpath:/templates/    # Suffix that gets appended to view names when building a URL.
    suffix: .html    # Template encoding.
    encoding: UTF-8    # Enable template caching.
    cache: false
    # Enable MVC Thymeleaf view resolution.
    enabled: true
    # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
    mode: HTML    # Check that the templates location exists.
    check-template-location: true
    servlet:      # Content-Type value.
      content-type: text/html

配置Spring MVC, WebMVCConfig .java

package com.lab.spring.boot.infrastructure.projects.config.web;import org.springframework.beans.factory.annotation.Configurable;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/**
 * 
 * Spring MVC WebConfig
 * 
 * @author Martin
 * @since 1.0.0
 */@Configurablepublic class WebMVCConfig implements WebMvcConfigurer {

}

首页Hello World 页面配置PortalController.java

package com.lab.spring.boot.infrastructure.projects.component.portal.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/**
 * 
 * Portal Controller
 * 
 * @author Martin
 * @since 1.0.0
 */@Controllerpublic class PortalController {

    @RequestMapping(value="/",method= RequestMethod.GET)
    public String index() {        return "index";
    }
}

最终项目目录结构如下:


webp

Finished



作者:数据服务
链接:https://www.jianshu.com/p/97e8740488f1


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP