关于springboot想必很多人都在使用,由于公司项目一直使用的是SpringMVC,所以自己抽空体验了一下springboot的简单使用。

  • 环境搭建

springbooot的环境搭建可以说很灵活,可以新建maven普通项目来手动搭建,当然也可以使用Spring的STS来搭建,由于IDE使用eclipse,所以就直接使用STS插件。

1. 在eclipse的Marketplace中搜索STS,后直接下载安装即可。

2.  安装好后,在新建项目中就可以看到springboot的相关内容,选择第二个新建springboot

3. 第三步就是选择对应的依赖,当然你可以将需要的依赖全部添加,也可以先添加当前需要的,随后用到什么在添加。


  • 代码结构

Springboot的启动很简单,如下:

 package com.yongcheng.liuyang;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

其中最主要的是@SpringBootApplication的注解,当然从启动过程也可以发现,springboot其实质还是运行在web容器中,

    

 

        接下来就写一个springmvc中经常用到的Controller很简单的测试下,当然前端页面我们选择了官方推荐的thymeleaf,

   所以首先要在pom.xml中导入这个依赖包,如下:

<!-- thymeleaf模板引擎包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>

controller的写法和springmvc一样,熟悉springmvc的基本不用多说,直接上代码:

 package com.yongcheng.liuyang.controller;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.yongcheng.liuyang.entity.AuthorSetting; @Controller
@RequestMapping(value="/")
public class UserLoginController { private static final Logger logger = LoggerFactory.getLogger(UserLoginController.class); @RequestMapping("/test")
public String testthymeleaf(Model model)
{
model.addAttribute("singlePerson", new AuthorSetting("zhangsan",18));
return "viewTest";
}
}

 由于我们需要返回页面所以使用的是@Controller注解,当然如果你要返回json数据,那么你的注解当然是@RestController

 接下就是前台的页面

 <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html" charset="UTF-8">
<link th:src="@{/css/bootstrap.min.css}" rel="stylesheet">
<link th:src="@{/css/bootstrap-theme.min.css}" rel="stylesheet"> <script th:src="@{/js/jquery-3.2.1.min.js}" type="text/javascript"></script>
<script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script> <title>thymeleaf简单实用</title>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">访问model</h3>
</div>
<div class="panel-body">
<span th:text="${singlePerson.name}"></span>
</div>
</div>
</body>
</html>

注意html文件中引入xmlns:th="http://www.thymeleaf.org",其次就是这里的动态加载使用th:dom选择,比如项目中的th:text,

URL使用@{},比如项目中css和js的引入。

这样子整个问题就算解决了吗?没有,了解springmvc的都知道,springmvc在解析JSP文件时需要配置对应的文件前后缀,比如如下的配置:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver" >
<!-- 自动添加到路径中的前缀 -->
<property name="prefix" value="/jsp/" />
<!-- 自动添加到路径中的后缀 -->
<property name="suffix" value=".jsp" />
</bean>

在springboot中一样也要配置这样的,其中springboot的所有配置信息都在application.properties或者application.yml文件中,

那么如何配置对应的thymeleaf的视图解析配置参数呢?

由于小编选择的是yml,配置参数如下:

spring:
thymeleaf:
prefix: "classpath:/templates/"
suffix: ".html"

当然如果你的项目结构绝对严谨的话,比如使用STS创建的项目,那么其实上面的是可以不配置的,具体原因可参见ThymeleafProperties类。

重要提示:

为了让项目正常加载bean,main函数所在类的一定要是其它类的父包,否则无法扫描到配置的bean。

好了,以上就是springboot+thymeleaf的简单使用。

												

springboot+thymeleaf简单使用的更多相关文章

  1. org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method service() cannot be found on com.my.blog.springboot.thymeleaf.util.MethodTest type

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题. 至于为什么已经写了一篇文章thymeleaf模板引擎调用java类中的方法,又多此一举的单独整 ...

  2. SpringBoot 搭建简单聊天室

    SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...

  3. springboot+thymeleaf+pageHelper带条件分页查询

    html层 <div> <a class="num"><b th:text="'共 '+ ${result.resultMap['pages ...

  4. SpringBoot thymeleaf使用方法,thymeleaf模板迭代

    SpringBoot thymeleaf使用方法,thymeleaf模板迭代 SpringBoot thymeleaf 循环List.Map ============================= ...

  5. SpringBoot thymeleaf模板页面没提示,SpringBoot thymeleaf模板插件安装

    SpringBoot thymeleaf模板插件安装 SpringBoot thymeleaf模板Html页面没提示 SpringBoot  thymeleaf模板页面没提示 SpringBoot t ...

  6. SpringBoot thymeleaf模板版本,thymeleaf模板更换版本

    SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...

  7. Springboot+Thymeleaf框架的button错误

    ---恢复内容开始--- 在做公司项目时,遇到了一个Springboot+Thymeleaf框架问题: 使用框架写网站时,没有标明type类型的button默认成了‘submit’类型,每次点击按钮都 ...

  8. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  9. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

随机推荐

  1. 检验两个随机序列的beta系数

    检验两个随机序列的beta系数 代码 def test_beta(loops=10): ''' 检验两个随机序列的beta系数 :loops: int, 循环次数, 每次循环会产生两个随机序列, 然后 ...

  2. 图片压缩之-JPEGCodec失效替换方案

    今天遇到一个405错误,提示Method not allowed ,一直以为是控制器出问题了,后来发现实际上是Jpeg库有问题.刚开始用这个库,没想到已经不推荐使用了.下面是网上找的解决方案.http ...

  3. myeclispe 一直运行debug问题

    window->preferences->Myeclipse->Servers->Tomcat 然后找到你的相应的Tomcat服务器的版本,选中然后展开其下面的子菜单会发现有个 ...

  4. Postfix 邮件服务 - postfix服务

    postfix 邮件服务 也是基于sendmail (需要关闭或卸载sendmail)postfix 是一个电子邮件服务器,它为了改良sendmail邮件服务器而产生的,并且它的配置文件比sendma ...

  5. 未能加载文件或程序集System.Web.Http.WebHost

    解决方案:只需要在项目的bin文件夹下放入下面三个dll. 将:C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies中的  ...

  6. HashMap、ArrayMap、SparseArray分析比较

    http://blog.csdn.net/chen_lifeng/article/details/52057427

  7. escape、encodeURI和encodeURIComponent的区别

    1.简单解释 简单来说,escape是对字符串(string)进行编码(而另外两种是对URL),作用是让它们在所有电脑上可读. 编码之后的效果是%XX或者%uXXXX这种形式. 其中 ASCII字母. ...

  8. CentOS和RedHat Linux的区别

    RHEL 在发行的时候,有两种方式.一种是二进制的发行方式,另外一种是源代码的发行方式. 无论是哪一种发行方式,你都可以免费获得(例如从网上下载),并再次发布.但如果你使用了他们的在线升级(包括补丁) ...

  9. 关于python中的类方法(classmethod)和静态方法(staticmethod)

    首先明确两点: a)python在创建类的时候,self指向的是类的实例而不是类属性! b)我们所创建的类的方法有两种作用,一种是改变类的属性,一种是改变实例的属性,这点一定要分清! c)我们在调用类 ...

  10. 一个优秀的 ring buffer 或 cycle buffer 的实现代码

    #define CIRCLE_BUFFSIZE 1024 * 1024#define min(x, y) ((x) < (y) ? (x) : (y)) struct cycle_buffer ...