为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
SpringBoot

搭建基本环境

目前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


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消