idea2019+springboot+thymeleaf模版demo

最近出去面试了一下,以前都是用eclipse+springmvc+mybaits自己整合的项目,对于springboot和idea了解甚少,但是去面试时发现构建一个简单的springmvc和mybatis自己构建起来真的让人头大,从今天开始要从0学习idea和springboot,每次的学习都会记录下来

(ps:我是自行学习,自己一点点摸索,跟其他人相同的话就是大家都是这样开始的吧)

网上有很多诸如此类的资料,我整理仅仅是我用的都是最新的开发工具和最新的jar包,仅作为参考

首先.我的idea是自己配置过maven仓库和jdk的,不会的小伙伴可以自行百度,这里没什么好说的.

先贴一个github上的地址:https://github.com/aplqik/idea2019-springboot,这里面包括了这次的代码和一点点下次的mybatis的代码

首先,建立一个新的项目.

选择spring Initializr,点击下一步

红框里的可以自行修改,但是要把jar改为war,因为做的有web页面,点击下一步

这里因为我们只是简单的建一个web项目,选择web即可.后面的依赖需要可以自己加,点击下一步

点击finish即可,目录结构如下

打开pom文件,加入thymeleaf模版依赖,pom文件如下,就加入了一个依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hex</groupId>
    <artifactId>spring_boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>spring_boot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 使用springboot默认的模版引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置下application.properties,就只配置下这些基本的就行了

#thymeleaf模版配置,缓存关闭,便于调试.
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5

自己新建一个package,然后新建一个类

这里需要注意的是springBoot会默认从启动类所在包开始,扫描当前包及其子包下的所有文件,如果你建的包不在spring_boot目录下,需要在启动类配置,我这里直接在com.hex.spring_boot下建立的包.所以不需要配置

建立controller类:

package com.hex.spring_boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("hello")
public class HelloWorldController {
    @RequestMapping("/world")
    public String doHello(Model model){
        model.addAttribute("hello","你好世界");
        return "hi";
    }
}

建立hi.html,这个themleaf的语法我真的用的少之又少,这是从晚上复制来的

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>你好世界</title>
</head>
<body>
    <h1 th:text="${hello}">Hello World</h1>
</body>
</html>

浏览器打开

注意事项:一般的小伙伴都能调的通controller,但是我调通controller(有打印,可以返回json)后却找不到html,真的是让我苦恼,百度下有说springboot2.0不行的,要用1.5,我是不太信,最后通过查证发现是maven的包有的没有找到.ps:小伙伴配置maven的时候如果是自己的仓库,如果用默认的下载慢,可以用阿里的中央仓库试下

修改maven/conf/setting.xml,可以自行百度如何修改maven中央仓库.


<mirror>
<id>AliRepo-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Mirror Name for the Alirepo.</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

解决办法: 项目右键,maven-reimport下,然后打开,file/project structure

点击modules,看看你那个jar包没有加载出来,就到仓库里删除掉这个包,重新让maven下载,一般都能解决.

下一篇我会介绍下,springboot+themleaf+mybatis的整合.做一个简单的curd.

发布了55 篇原创文章 · 获赞 17 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/he_xiao123/article/details/100538141
今日推荐