jsp和模板引擎(一文带你学习SpringBoot(下))java教程 / Java Web开发中的模板引擎选择与使用...

wufei123 发布于 2024-06-08 阅读(6)

七、模板引擎1.简介目前Java Web开发推荐使用模板引擎,不建议使用jsp页面jsp的缺点:本质时Servlet,需要后台进行编译,效率较低模板引擎:不需要编译,速度快常用的模板引擎:Freemarker、Thymeleaf等

SpringBoot推荐Thymeleaf,且默认不支持jsp,因为jsp必须要打成war包补充:目前主流的web开发更推荐前后端分离,前端使用MVVM框架,Vue.js、Angular、React等2.Thymeleaf的使用

步骤:1.添加Thymeleaf的依赖org.springframework.bootspring-boot-starter-thymeleaf

2.将html页面放到templates下templates下的html不能直接访问,需要使用Controller跳转,由Thymeleaf进行渲染ThymeleafAutoConfiguration—>ThymeleafProperties

publicstaticfinal String DEFAULT_PREFIX = "classpath:/templates/";publicstaticfinal String DEFAULT_SUFFIX =

".html"; 默认拼接前缀和后缀​ 3.使用thymeleaf

>Title

success

​ 4.修改页面后,让其实时生效​ 由于thymeleaf默认启用了缓存,将缓存禁用掉#禁用thymeleaf的缓存spring.thymeleaf.cache=

false​ 补充:还需要开启idea的自动编译,idea默认保存时不会自动编译

3.语法规则3.1 常用属性th:text、th:utext设置元素中的文本内容th:text对特殊字符进行转义,等价于内联方式[[${ }]]th:utext不对特殊字符集进行转义,等价于内联方式[(${ })]

>Titleaaa

>bbb

[[${hello}]]aaa
[(${hello})]bbb

th:html原生属性用来替换指定的html原生属性的值@RequestMapping("/test2")public String test2(Model model){ model.addAttribute(

"hello", "你好"); model.addAttribute("id", "mydiv"); model.addAttribute("title", "this is a div"

); return"result";}

>div
th:if、th:unless、th:switch、th:case条件判断,类似于if="${age>=18}">成年 姓名:

type="text"th:field="*{name}">

年龄:

="修改">th:fragment声明代码片段,常用于页面头部和尾部的引入 这是页面的头部,导航

[[${users.size()}]]个
使用内置的基本对象,如session和application<

divth:text="${application.hobby}">使用内置的工具对象,如#strings、#dates、#arrays、#lists、#maps等

>

th:text="${#dates.create(2018, 10, 14)}">

>*{} 选择表达式(星号表达式)

>@{} url表达式Title

>查询指定的用户信息商品列表

>运算符eq gt le == != 三目运算符4.热部署使用SpringBoot提供的devtools实现热部署原理:实时监控classpath下文件的变化,如果发生变化自动重启配置:添加devtools依赖

org.springframework.bootspring-boot-devtools

true八、扩展默认的SpringMVC功能1.简介​ 以前在SpringMVC中可以通过如下代码进行视图跳转和拦截器:

path="/showLogin"view-name="login"/>

class="com.luyi.interceptor.HelloInterceptor"/>SpringBoot默认配置默认没有提供以上功能,需要自己扩展,使用WebMvcConfigurer接口

2.基本操作​ 步骤:​ 1.定义一个配置类,实现WebMvcConfigurer接口​ 2.实现需要的方法/***Author:LuYi*Date:2019/10/2917:58*Description:

扩展默认的SpringMVC的功能*要求:*1.将该类标记为配置类*2.实现WebMvcConfigurer接口*3.根据需要实现接口中相应的方法**注意:这个接口中的方法都添加了jdk1.8中的default方法修饰,不强制实现所有方法(jdk1.8新特性)

*在SpringBoot1.0中是继承WebMvcConfigurerAdapter类,SpringBoot2.0是基于jdk1.8的,*所以通过实现WebMvcConfigurer的方式*///将该类设置为配置类@Configurationpublic

classCustomMvcConfigimplementsWebMvcConfigurer{//添加ViewController@OverridepublicvoidaddViewControllers(ViewControllerRegistry

registry){//将访问login页面的url设置为showLoginregistry.addViewController("/showLogin").setViewName("login");}

@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(newMyInterceptor()).addPathPatterns("/**")

.excludePathPatterns("/test2");}}九、全局异常处理1.简介​ 当程序出现异常时进行全局处理,SpringBoot默认的异常信息提示:Whitelabel Error Page

两种方式:定义错误码页面定义异常通知2.定义错误码页面​ 创建 错误状态码.html页面,放到templates/error目录中,当发生错误时会自动到该目录下查找对应的错误页面​ 可以创建如 4xx.html或5xx.html页面,用来匹配所有该类型的错误(会优先进行精确匹配

Title

5xx错误

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

河南中青旅行社综合资讯 奇遇综合资讯 盛世蓟州综合资讯 综合资讯 游戏百科综合资讯 新闻14191