iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用
  • 135
分享到

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

2023-06-02 22:06:49 135人浏览 八月长安
摘要

Spring Boot中,静态资源(CSS、js、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。模板文件默认放在

Spring Boot中,

  • 静态资源(CSSjs、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。

  • 模板文件默认放在 templates目录下

  •  Spring boot支持使用模板来开发WEB应用,支持的模板类型包括

     FreeMarker

       Groovy

       Thymeleaf

        Mustache

Spring boot不建议使用jsp开发web。

本文使用Thymeleaf作为模板引擎来开发web项目

一、在application.properties中设置Thymeleaf相关属性

# Check that the template exists before rendering it.spring.thymeleaf.check-template=true# Check that the templates location exists.spring.thymeleaf.check-template-location=true# Content-Type value written to Http responses.spring.thymeleaf.content-type=text/html# Enable Thymeleaf view resolution for Web frameworks.spring.thymeleaf.enable=true# Template files encoding.spring.thymeleaf.encoding=UTF-8# Comma-separated list of view names that should be excluded from resolution.spring.thymeleaf.exclude-view-names=index# Prefix that gets prepended to view names when building a URL.spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html# Template mode to be applied to templates. See also StandardTemplateModeHandlers.spring.thymeleaf.mode=HTML5# Disable template caching.spring.thymeleaf.cache=false

二、在pom.xml添加依赖

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

三、在resources目录下创建templates文件夹,并添加模板html

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

test.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body>姓名:<span th:text="${user.username}"></span></br>邮箱:<span th:text="${user.email}"></span></br>昵称:<span th:text="${user.nickname}"></span></body></html>

四、增加Controller

@Controllerpublic class TemplatesController {@Autowiredprivate UserService userService;@RequestMapping("/index")String test(ModelMap map) {map.addAttribute("key", "thymeleaf");return "index";}  @RequestMapping("/test")public String testThymeleaf(ModelMap map) {map.addAttribute("user", userService.selectByPrimaryKey(1));return "test";    }}

五、运行结果

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

到这里,Spring Boot + mybatis +Spring MVC环境就搭建完成了

完整环境下载地址:https://GitHub.com/CatherineHu/Spring-Boot-Mybatis-MVC 

--结束END--

本文标题: Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

本文链接: https://www.lsjlt.com/news/231537.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用
    Spring Boot中,静态资源(css、js、图片等)默认放在resources/static下面。如果要修改默认存放目录,可以通过设置属性 spring.mvc.static-path-pattern来实现。模板文件默认放在...
    99+
    2023-06-02
  • Spring Boot + Mybatis + Spring MVC环境配置(二):Mybatis Generator配置
    一、在Eclipse中安装mybatis generator     菜单选择:Help->Eclipse Marketplace二、 创建generatorConfig.xml配置文档配置好的gener...
    99+
    2023-06-02
  • Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置
    小编给大家分享一下Spring Boot + Mybatis + Spring MVC环境配置中DataSource如何配置,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解...
    99+
    2023-06-02
  • Spring Boot + Mybatis + Spring MVC环境配置中Spring Boot如何实现初始化以及依赖添加
    这篇文章将为大家详细讲解有关Spring Boot + Mybatis + Spring MVC环境配置中Spring Boot如何实现初始化以及依赖添加,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。看一...
    99+
    2023-06-02
  • Spring Boot多模块多环境的配置方法是什么
    这篇文章主要介绍“Spring Boot多模块多环境的配置方法是什么”,在日常操作中,相信很多人在Spring Boot多模块多环境的配置方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring ...
    99+
    2023-06-16
  • 深入理解Spring MVC概要与环境配置
    一、MVC概要MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑、数据、显示分离的方法组织代码,MVC主要作用是降低了视图与业务逻辑间的双向偶合。MVC不是一种设计模式,...
    99+
    2023-05-31
    spring mvc 环境
  • 怎么对Spring Boot配置文件进行多环境配置
    这期内容当中小编将会给大家带来有关怎么对Spring Boot配置文件进行多环境配置,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。一. 多环境配置的好处:不同环境配置可以配置不同的参数~便于部署,提高效率...
    99+
    2023-05-31
    springboot spring boo bo
  • Spring Boot使用和配置Druid
    引入依赖包<!--druid--><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artif...
    99+
    2023-05-31
    spring boot druid
  • 如何使用Spring Boot thymeleaf模板引擎
    本篇内容主要讲解“如何使用Spring Boot thymeleaf模板引擎”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Spring Boot thymeleaf模板引擎”吧!在早期开...
    99+
    2023-06-07
  • thymeleaf模板如何在spring boot中使用
    这篇文章将为大家详细讲解有关thymeleaf模板如何在spring boot中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。前言Thymeleaf 是一个跟 Velocity、Free...
    99+
    2023-05-31
    springboot thymeleaf
  • Spring Boot 如何实现与MyBatis搭配使用
    本篇文章给大家分享的是有关Spring Boot 如何实现与MyBatis搭配使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Spring Boot是由Pivotal团队提供...
    99+
    2023-05-31
    springboot 搭配 mybatis
  • spring Boot怎么与Thymeleaf模板引擎结合使用
    这篇文章给大家介绍spring Boot怎么与Thymeleaf模板引擎结合使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Thymeleaf:Thymeleaf是一个java类库,他是一个xml/xhtml/htm...
    99+
    2023-05-31
    springboot thymeleaf
  • mybatis spring配置SqlSessionTemplate的使用方法
    这篇文章主要讲解了“mybatis spring配置SqlSessionTemplate的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis spring配置SqlSess...
    99+
    2023-06-20
  • 使用Spring Boot如何配置maven文件
    使用Spring Boot如何配置maven文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.配置maven文件pom.xml<&#63;xml versio...
    99+
    2023-05-31
    springboot maven
  • mybatis spring配置SqlSessionTemplate的使用方式
    mybatis spring配置SqlSessionTemplate使用 1.application.xml配置 <?xml version="1.0" encod...
    99+
    2024-04-02
  • 使用Spring如何搭建一个SpringMVC与MyBatis环境
    这篇文章将为大家详细讲解有关使用Spring如何搭建一个SpringMVC与MyBatis环境,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1.对原生态jdbc程序中问题总结1 jdbc程序...
    99+
    2023-05-31
    mybatis spring springmvc
  • 如何使用spring boot starter redis配置文件
    本篇文章为大家展示了如何使用spring boot starter redis配置文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。spring-boot-starter-Redis主要是通过配置R...
    99+
    2023-05-31
    springboot starter redis
  • Cli如何在spring Boot中配置并使用
    Cli如何在spring Boot中配置并使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。SpringBootCLI是一个命令行工具,可用于快速搭建基于spring的原型...
    99+
    2023-05-31
    springboot cli
  • springboot怎么使用logback-spring配置日志格式,并分环境配置
    这篇文章主要介绍了springboot怎么使用logback-spring配置日志格式,并分环境配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。配置不生效的解决办法注意:如...
    99+
    2023-06-20
  • 详解spring-boot actuator(监控)配置和使用
    在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:1、引入依赖:<dependency> <groupId>org...
    99+
    2023-05-31
    spring boot actuator
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作