3-Spring-Boot框架开发-SpringBoot视图技术_第1页
3-Spring-Boot框架开发-SpringBoot视图技术_第2页
3-Spring-Boot框架开发-SpringBoot视图技术_第3页
3-Spring-Boot框架开发-SpringBoot视图技术_第4页
3-Spring-Boot框架开发-SpringBoot视图技术_第5页
已阅读5页,还剩27页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SpringBoot框架开发—SpringBoot视图技术CONTENTSThymeleaf模板01.静态资源访问02.Thymeleaf应用03.Thymeleaf基本语法04.集成SpringMVC05.本节知识点00知识点了解掌握熟练精通Thymeleaf简介√

静态资源访问

√

使用Thymeleaf编写页面

√Thymeleaf基本语法

√

集成SpringMVC√

PART01Thymeleaf模板Thymeleaf模板01Thymeleaf是Java服务端的模板引擎,可以使用浏览器直接打开,因为可以忽略掉拓展属性,相当于打开原生页面,给前端人员也带来一定的便利。相较与其他的模板引擎,它有如下三个特点:1)Thymeleaf可以实现页面的静态显示和动态显示,即可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。2)Thymeleaf开箱即用的特性。它提供标准和Spring标准两种方言,可以直接套用模板实现JSTL、OGNL表达式效果,避免每天套模板、改JSTL、改标签,同时开发人员也可以扩展和创建自定义的方言。3)Thymeleaf提供Spring标准方言和一个与SpringMVC完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。PART02静态资源访问静态资源访问02classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public//:当前项目的根路径

其中优先级按照从上到下,例如,static目录下有个index.html,public目录下也有个index.html,则优先会加载static下面的index.html。PART03Thymeleaf应用Thymeleaf应用03【实例3-10】【任务要求】

在SpringBoot项目中,使用Thymeleaf模板引擎实现登录页面login.html,点击页面中的登录按钮,跳转到欢迎页面hello.html。

Thymeleaf应用03【任务实现】创建SpringBoot项目,引入Thymeleaf依赖。<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>在pom.xml文件中添加Thymeleaf依赖,注意是boot下的spring-boot-starter-thymeleaf依赖包。Thymeleaf应用03【任务实现】2.编辑配置文件,添加Thymeleaf配置。#thymeleaf

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.check-template-location=true

spring.thymeleaf.suffix=.html

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.content-type=text/html

spring.thymeleaf.mode=HTML5

spring.thymeleaf.cache=false在perties文件中,添加相关配置项,代码如下:Thymeleaf应用03【任务实现】3.创建模板文件……

<body>

<formclass="text-center"th:action="@{/logout}">

<h1class="h3mb-3font-weight-normal">请登录</h1>

<inputclass="form-control"type="text"th:placeholder="请输入用户名"autofocus="">

<inputclass="form-control"type="password"th:placeholder="请输入密码">

<buttonclass="btnbtn-lgbtn-primarybtn-block"type="submit">登录</button>

<pclass="mt-5mb-3text-muted"th:text="${currentYear}">2018</p>

</form>

</body>

</html>在resources文件夹下新增templates目录,用于存放模板文件,新增login.html,代码如下:Thymeleaf应用03【任务实现】3.创建模板文件<!DOCTYPEhtml>

<htmllang="en"xmlns:th="">

<head>

<metacharset="UTF-8">

<title>欢迎页面</title>

<linkth:href="@{/css/bootstrap.min.css}"rel="stylesheet">

<linkth:href="@{/css/signin.css}"rel="stylesheet">

</head>

<body>

<pth:text="${hello}">显示欢迎信息</p>

</body>

</html>新建Hello.html,用于登录成功后的跳转,代码如下:Thymeleaf应用03【任务实现】4.编写控制类@Controller

publicclassLoginController{

@RequestMapping("/toLogin")

publicStringtoLoginPage(Modelmodel){

model.addAttribute("currentYear",

Calendar.getInstance().get(Calendar.YEAR));

return"login";

}

@RequestMapping("/logout")

publicStringlogout(Modelmodel){

model.addAttribute("hello","欢迎进入Thymeleaf页面");

return"hello";

}

}在java目录下新建包.controller,并创建控制类LoginController.java:Thymeleaf应用03【任务实现】5.编写启动类

@SpringBootApplication

publicclassViewApplication{

publicstaticvoidmain(String[]args){

SpringApplication.run(ViewApplication.class,args);

}

}启动类ViewApplication.java不需要特别的设置,代码如下:Thymeleaf应用03【任务实现】6.验证项目启动后,在浏览器端输入以下url地址:http://localhost:8080/toLogin,就可以访问登录页面:PART04Thymeleaf基本语法Thymeleaf基本语法04<htmlxmlns:th="">这样,下文才能正确使用th:*形式的标签。引入命名空间Thymeleaf基本语法04<!--/*@thymesVarid="hello"type="java.lang.String"*/--><pth:text="${hello}">显示欢迎信息</p>通过${…}进行取值,这和ONGL表达式语法一致。获取变量值${...}Thymeleaf基本语法04<divth:object="${session.user}">

<p>Name:<spanth:text="*{name}">Susi</span></p>

<p>c:<spanth:text="*{age}">30</span></p>

<p>Nationality:<spanth:text="*{nationality}">China</span></p>

</div>

选择表达式就像变量表达式一样,它们不是在整个上下文变量映射上执行,而是在先前选择的对象。选择变量表达式*{...}Thymeleaf基本语法04

消息表达式的作用,就是用于对国际化功能的支持。所谓的国际化功能就是指,根据浏览器的编码,返回对应编码的内容的支持。消息表达式(1)编写国际化文件

在resources下面增加i18n目录,并能添加国际化文件perties、login_zh_CN.properties、login_en_US.properties。Thymeleaf基本语法04消息表达式(1)编写国际化文件login.tip=请登录

login.username=用户名

login.password=密码

login.rememberme=记住我

login.button=登录pertieslogin_zh_CN.propertieslogin_en_US.propertieslogin.tip=Pleasesignin

login.username=Username

login.password=Password

login.rememberme=Rememberme

login.button=Loginlogin.tip=请登录

login.username=用户名

login.password=密码

login.rememberme=记住我

login.button=登录Thymeleaf基本语法04消息表达式(2)编辑配置文件

在perties全局配置文件中添加国际化文件基础名设置。#配置国际化文件基础名

spring.messages.basename=i18n.loginThymeleaf基本语法04消息表达式(3)页面国际化使用

在上一小节的login.html文件中添加消息表达式。<h1class="h3mb-3font-weight-normal"th:text="#{login.tip}">请登录</h1>

<inputclass="form-control"type="text"

th:placeholder="#{login.username}"autofocus="">

<inputclass="form-control"type="password"

th:placeholder="#{login.password}">

<buttonclass="btnbtn-lgbtn-primarybtn-block"type="submit"

th:text="#{login.button}">登录</buttth:text="${currentYear}">on>

<pclass="mt-5mb-3text-muted"2018</p>

<aclass="btnbtn-sm"th:href="@{/toLogin(l='zh_CN')}">中文</a>

<aclass="btnbtn-sm"th:href="@{/toLogin(l='en_US')}">英文</a>Thymeleaf基本语法04<head>……<!--引入CSS和JS文件--><linkth:href="@{/css/bootstrap.min.css}"rel="stylesheet">

<linkth:href="@{/css/signin.css}"rel="stylesheet">

</head>

<body><!—Form表单提交-->

<formclass="text-center"th:action="@{/logout}">

……</body>

对于HTML中的link、src、href等语法使用的场景,Thymeleaf相应的标签有th:href和th:src。链接表达式:@{…}Thymeleaf基本语法04th:fragment:自定义片段名。

th:replace:不要自己的主标签,保留th:fragment的主标签。

th:insert:保留自己的主标签,保留th:fragment的主标签。

th:include:保留自己的主标签,不要th:fragment的主标签。~{...}:片段表达式Thymeleaf基本语法04常用标签标签作用th:insert页面片段包含(类似JSP中的include标签)th:replace页面片段包含(类似JSP中的include标签)th:each元素遍历(类似JSP中的c:forEach标签)th:if条件判断,如果为真th:unless条件判断,进行选择性匹配th:switch条件判断,进行选择性匹配th:case条件判断,进行选择性匹配th:object变量声明th:with变量声明Thymeleaf基本语法04常用标签标签作用th:attr通用属性修改th:attrprepend通用属性修改,将计算结果追加前缀到现有属性值th:value属性值修改,指定标签属性值th:href用于设定链接地址th:src用于设定链接地址th:text用于指定标签显示的文本内容th:utext用于指定标签显示的文本内容,对特殊标签不转义th:fragment声明片段th:remove移除片段PART05集成SpringMVC集成SpringMVC

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论