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

手把手教你如何使用Java语言开发门户网站,后续将不断迭代更新,针对当前主流门户网站建设应该考虑的问题常见的解决方法及实践技巧

本章节目标
  1. 使用Eclipse工具,基于Maven搭建由Spring Boot +Spring MVC +Thymleaf 构建Web门户基础框架
  2. 采用yml方式进行配置
搭建基本环境

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

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

打开eclipse , File > New > Maven Project
New Maven project

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

选择Next >, 输入项目信息
2018-9-1 12-46-33.png

选择Finish完成
spring-boot-infrastructure-projects

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

Eclipse > 文件 > 新建 > 新建 Source Folder
src/mian/resources

至此,一个简单的Maven项目构建完成,截图如下
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
 */
@Configurable
public 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
 */
@Controller
public class PortalController {

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

最终项目目录结构如下:
Finished

启动运行: Application.java 右键启动运行
启动成功  端口:8080

打开浏览器地址,输入地址访问:
http://localhost:8080/

好了,一个简单的Spring Boot + Spring MVC + Thymleaf 基础框架就搭建完成了,后续将在此基础之上做其他组件的扩展。

源代码已经更新到Github上

项目地址:spring-boot-infrastructure-projects
Git检出地址: https://github.com/DataZhenTech/spring-boot-infrastructure-projects.git

有问题,进群借一步说话  QQ群:559547981

作者:
    Martin
描述:
    全观数据栈,焦距行业、科技、技术、区域性大数据


-- 看完这张图,就到此结束吧! --

– 看完这张图,就到此结束吧! –

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ma Ding

喜欢的小伙伴,+个鸡腿吧.

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值