目录
一、springboot的前端应用
前端的使用:
一、基于springboot使用thymeleaf模板
thymeleaf实现循环:利用th:each
二、springboot中对于jsp的使用:
一、springboot的前端应用
1、主要用来打造分布式应用,微服务是重点
2、对于单体应用(不大),可能前端与后台应用存在于一个项目中。
3、如果项目是微服务项目,它应该是前后端分离的项目。
前端的使用:
1、springboot原本是不支持jsp的。
2、springboot它其实强烈建议使用模板技术来替换jsp。
freemark模板引擎,可以把模板文件和模型数据经过加工后生成静态页面。
那么我们的springboot建议使用thymeleaf模板技术。
一、基于springboot使用thymeleaf模板
文件放置位置如下:
thymeleaf实现循环:利用th:each
th:text表示取值
那么我们可以看到,其中即有静态数据张三又有动态的$的取值。
其实它也有if,choose条件表达式。
控制类:
使用modelMap将属性值存到域里面。
@Controller
public class MyController {
@RequestMapping("/demo4")
public String demo(ModelMap modelMap){
User user1 = new User();
user1.setId(100);
user1.setUserName("XXXXX");
User user2 = new User();
user2.setId(200);
user2.setUserName("YYYYY");
ArrayList users = new ArrayList<>();
users.add(user1);
users.add(user2);
modelMap.addAttribute("users",users);
// 返回模板名称(就是classpath:/templates/目录下的html文件名)
return "user";
}
}
导入依赖:
org.springframework.boot
spring-boot-starter-thymeleaf
下面演示访问结果:成功显示数据到thymeleaf模板上
二、springboot中对于jsp的使用:
- 首先第一步利用maven脚手架创建一个web的项目。
- 导入依赖
4.0.0
com.lay
demo5
1.0-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
javax.servlet
javax.servlet-api
javax.servlet
jstl
org.springframework.boot
spring-boot-starter-tomcat
org.apache.tomcat.embed
tomcat-embed-jasper
src/main/resources
***.*
org.springframework.boot
spring-boot-maven-plugin
com.lay.demo5.demo5Application
repackage
- 项目结构如下:
配置文件中指明文件访问的前后缀
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
myjsp.jsp文件:
<%--
Created by IntelliJ IDEA.
User: 世佳
Date: 2022/5/2
Time: 15:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${name},你好!
前端控制器代码:
@Controller
public class MyController {
@RequestMapping("/demo5")
public String demo(Model model){
model.addAttribute("name","汤世佳");
return "myjsp";
}
}
访问8080端口:可以看到成功整合jsp页面。



