近来在学习SprintBoot +Thymeleaf +Maven搭建自己的博客系统,故在学习过程中在此记录一下,也希望能给广大正在学习SprintBoot和Thymeleaf的朋友们一个参考。

以下是目录结构:

效果:

1、创建一个Springboot 工程,具体步骤这个网上例子一大把,选择WEB,thymeleaf框架就行了,以下是pom.xml的内容

  1. <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion> <groupId>com.spring.blog</groupId>
    <artifactId>myblog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging> <name>myblog</name>
    <description>myblog project for Spring Boot</description> <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent> <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <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-thymeleaf</artifactId>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins> </build>
    </project>

2、创建用户类(避免太长省去了get set方法,可下载完整项目)

package com.spring.boot.blog.blog.domain;

/**
* Created by 龙烨 on 2018/7/16.
*/
public class User {
private Long id; // 用户的唯一标识
private String name;
private String email; public User(){//无参数 }
public User(Long id,String name,String email){
this.id=id;
this.name=name;
this.email=email;
} }

3、创建接口及实现方法,应为这里没有连接数据库,故使用内存

package com.spring.boot.blog.blog.repository;

import com.spring.boot.blog.blog.domain.User;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong; /**
* Created by 龙烨 on 2018/7/16.
*/
@Service
public class UserRepositoryImpl implements UserRepository { private static AtomicLong counter=new AtomicLong();
private final ConcurrentMap<Long,User> userMap=new ConcurrentHashMap<>(); @Override
public User saveOrUpateUser(User user) {
Long id=user.getId();
if(id==null){//新建
id=counter.incrementAndGet();
user.setId(id);
}
this.userMap.put(id,user);
return user;
} @Override
public void deleteUser(Long id) {
this.userMap.remove(id); } @Override
public User getUserById(Long id) { return this.userMap.get(id);
} @Override
public List<User> listUser() {
return new ArrayList<User>(this.userMap.values());
}
}

  

4 、创建控制类(避免太长,部分内容,下载源码可查看完整)

@RestController
@RequestMapping("/users")
public class UserController { @Autowired
private UserRepository userRepository;
/**
* 查询所以用户
* @param model
* @return
*/
@GetMapping
public ModelAndView list(Model model){
model.addAttribute("userList",userRepository.listUser());
model.addAttribute("title","用户管理");
return new ModelAndView("users/list","userModel",model);
} /**
* 查询单个用户
* @param model
* @return
*/
@GetMapping("{id}")
public ModelAndView view(@PathVariable("id") Long id, Model model){
User user=userRepository.getUserById(id);
model.addAttribute("user",user);
model.addAttribute("title","查看用户");
return new ModelAndView("users/view","userModel",model);
} /**
* 创建表达页面
* @param model
* @return
*/
@GetMapping("/form")
public ModelAndView createForm( Model model){
model.addAttribute("user",new User());
model.addAttribute("title","创建用户");
return new ModelAndView("users/form","userModel",model);
} @PostMapping
public ModelAndView saveOrUpdateUser(User user){
user=userRepository.saveOrUpateUser(user);
return new ModelAndView("redirect:/users");
}

5、resources 下创建文件:

application.properties 增加以下内容:

# THYMELEAF
spring.thymeleaf.encoding=UTF-8
# 热部署静态文件
spring.thymeleaf.cache=false
# 使用HTML5标准
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=classpath:/templates/

  

源码下载:源码

博客系统实战——SprintBoot 集成Thymeleaf 实现用户增删查改(含源码)的更多相关文章

  1. Spring Boot企业级博客系统实战视频教程

    欢迎关注我的微信公众号:"Java面试通关手册" 回复关键字" springboot "免费领取(一个有温度的微信公众号,期待与你共同进步~~~坚持原创,分享美 ...

  2. 从零开始,搭建博客系统MVC5+EF6搭建框架(4)下,前后台布局实现、发布博客以及展示。

    一.博客系统进度回顾 目前已经完成了,前台展示,以及后台发布的功能,最近都在做这个,其实我在国庆的时候就可以弄完的,但是每天自己弄,突然最后国庆2天,连电脑都不想碰,所以就一直拖着,上一篇写了前端实现 ...

  3. 【干货】利用MVC5+EF6搭建博客系统(四)(下)前后台布局实现、发布博客以及展示

    二.博客系统后台布局实现 2.1.这里所用的是MVC的布局页来实现的,后台主要分为三部分:导航.菜单.主要内容 代码实现: 这里把后台单独放在一个区域里面,所以我这里建立一个admin的区域 在布局页 ...

  4. Linux之博客系统的搭建

    博客系统 三种配置:php+nginx+mysql 搭建步骤 改主机名 hostnamectl set-hostname lnmp 传入LNMP压缩包于root下(略) 配置网络(略)及yum源 先检 ...

  5. 【ASP.NET实战教程】基于ASP.NET技术下多用户博客系统全程实战开发(NNblog)

    岁末主推:牛牛老师主讲,多用户博客系统,基于ASP.NET技术,年后将带来移动业务平台项目项目目标: 打造个性品牌Blogo,定制多用户博客 为每一个博客用户提供个性化的 blogo解决方案,打造精品 ...

  6. 项目实战(连载):基于Angular2+Mongodb+Node技术实现的多用户博客系统教程(2)

    本章主要讲什么(一句话)?   <项目实战:基于Angular2+Mongodb+Node技术实现的多用户博客系统教程(2)> -- 基于MongoDB的MyBlog数据库知识技术储备(上 ...

  7. Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦

    个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...

  8. 欢迎阅读daxnet的新博客:一个基于Microsoft Azure、ASP.NET Core和Docker的博客系统

    2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客.当然,我写博客也不是从2008年才开始的,在更早时候,也在CSDN和系统分析员协会(之后名为"希赛网" ...

  9. 一个基于Microsoft Azure、ASP.NET Core和Docker的博客系统

    2008年11月,我在博客园开通了个人帐号,并在博客园发表了自己的第一篇博客.当然,我写博客也不是从2008年才开始的,在更早时候,也在CSDN和系统分析员协会(之后名为“希赛网”)个人空间发布过一些 ...

随机推荐

  1. AWK工具的用法

    基本格式 awk '{commands}' filename 或者 stdin | awk '{commands}' 以下,均简写为awk '{commands}'的形式 commands的用法 co ...

  2. 【MongoDB-MongoVUE图像管理工具】

    介绍一款很不错的开源的MongoDB图形化管理工具:MongoVUE . MongoVUE 1.6.9 破解版,下载地址.    

  3. 爬虫、网页分析解析辅助工具 Xpath-helper

    每一个写爬虫.或者是做网页分析的人,相信都会因为在定位.获取xpath路径上花费大量的时间,甚至有时候当爬虫框架成熟之后,基本上主要的时间都花费在了页面的解析上.在没有这些辅助工具的日子里,我们只能通 ...

  4. JUC源码阅读参考文章

    (飞哥)http://brokendreams.iteye.com/blog/2252081 (熊猫)http://blog.csdn.net/xiaoxufox/article/details/51 ...

  5. easyui修改提示窗

    1.将文本框type修改成 password 2.easyui中的js

  6. 推荐《Java编程思想》

    最近看了(美)Bruce Eckel 著<Java编程思想>,还没有看完,但是极力推荐.尤其是学完一遍基础之后,再看很有感觉.之前学习基础的时候就买了这本书,那时候也真心看不下去,包括现在 ...

  7. java数组创建

    java数组创建:int[] number = new int[10]; int[]:表明这是一个数组 new int[10]:给前面的数组类型的number变量分配10个int类型的空间大小

  8. python 系列文章汇总(持续更新...)

    引言 不知不觉已经写了好几篇 python 相关的随笔了,从刚开始的门外汉到现在已经对 python 有一些入门了,时间也已经过去了一个多月. 写博客真是好处多多,不仅能提供整理自己学习的知识点,梳理 ...

  9. Only fullscreen activities can request orientation

    问题 当我们把targetSdkVersion升级到27,buildToolsVersion和相关的support library升级到27.0.2后,在Android 8.0(API level 2 ...

  10. node 搭建静态服务

    对于Node.js新手,搭建一个静态资源服务器是个不错的锻炼,从最简单的返回文件或错误开始,渐进增强,还可以逐步加深对http的理解. 基本功能 不急着写下第一行代码,而是先梳理一下就基本功能而言有哪 ...