java spring boot2 springMVC thymeleaf 整合案例

java 安装配置等相关

java 版本 1.8.x

https://blog.csdn.net/fenglailea/article/details/82152210

风.foxwho

IDEA 安装配置

IDEA 创建项目

Create new Project

java spring boot2 springMVC thymeleaf 整合案例_第1张图片

java spring boot2 springMVC thymeleaf 整合案例_第2张图片

java spring boot2 springMVC thymeleaf 整合案例_第3张图片

java spring boot2 springMVC thymeleaf 整合案例_第4张图片

配置 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.0modelVersion>

    <groupId>com.foxwhogroupId>
    <artifactId>foxartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.0.M4version>
    parent>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtmlgroupId>
            <artifactId>nekohtmlartifactId>
            <version>1.9.22version>
        dependency>
    dependencies>

    
    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        repository>
        <repository>
            <id>maven-aliid>
            <url>http://maven.aliyun.com/nexus/content/groups/public//url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
                <updatePolicy>alwaysupdatePolicy>
                <checksumPolicy>failchecksumPolicy>
            snapshots>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
        pluginRepository>
        <pluginRepository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        pluginRepository>
    pluginRepositories>
project>

等待 maven 自动加载创建包

创建包

src/main/java下创建包com.foxwho(java文件夹右击,菜单New->Package)

创建入口

在包com.foxwho下创建 Class 名称为 Application,内容如下

package com.foxwho;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如图
java spring boot2 springMVC thymeleaf 整合案例_第5张图片

创建控制器 IndexController

在包com.foxwho下创建包controller,在包controller创建 Class IndexController 内容如下

package com.foxwho.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

创建 index.html

注意:html标签中xmlns:th="http://www.thymeleaf.org" 必须存在,否则 thymeleaf 模板不识别
thymeleaf 模板默认存储根目录是 resources/templates文件夹中

resources 文件下创建templates文件夹,在templates下创建 index.html
内容如下


<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
 这是 IndexController
body>
html>

创建控制器 HelloController

在包controller创建 Class HelloController 内容如下

package com.foxwho.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @RequestMapping("/say.html")
    public @ResponseBody String say() {
        return "Spring Boot => say.html";
    }
}

配置 application.yml

resources 创建 application.yml文件,内容如下

spring:
  thymeleaf:
    cache: false # 开发时关闭缓存,不然没法看到实时页面
    mode: LEGACYHTML5 # 用非严格的 HTML   ,HTML5
    encoding: UTF-8
    servlet:
      content-type: text/html
  #prefix: classpath:

server:
  port: 8080 #更改tomcat端口

运行

java spring boot2 springMVC thymeleaf 整合案例_第6张图片

java spring boot2 springMVC thymeleaf 整合案例_第7张图片

java spring boot2 springMVC thymeleaf 整合案例_第8张图片

java spring boot2 springMVC thymeleaf 整合案例_第9张图片

点击 小乌龟 执行
java spring boot2 springMVC thymeleaf 整合案例_第10张图片

浏览器中打开http://localhost:8080/
java spring boot2 springMVC thymeleaf 整合案例_第11张图片

浏览器中打开http://localhost:8080/say.html
java spring boot2 springMVC thymeleaf 整合案例_第12张图片

源码地址
https://github.com/foxiswho/java-spring-boot2-springMVC-thymeleaf-demo

你可能感兴趣的:(java)