sringboot(四)

目录

一、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将属性值存到域里面。

/**
 * @author Dragon code!
 * @create 2022-05-02 15:15
 */
@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<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        modelMap.addAttribute("users",users);
        // 返回模板名称(就是classpath:/templates/目录下的html文件名)
        return "user";
    }
}

导入依赖:

        <!--创建的模板就放在templates下面-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

下面演示访问结果:成功显示数据到thymeleaf模板上

二、springboot中对于jsp的使用:

  • 首先第一步利用maven脚手架创建一个web的项目。
  • 导入依赖
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lay</groupId>
  <artifactId>demo5</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <!-- 添加servlet依赖模块 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <!-- 添加jstl标签库依赖模块 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
    </dependency>
    <!--添加tomcat依赖模块.-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/webapp</directory>
        <targetPath>META-INF/resources/</targetPath>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>com.lay.demo5.demo5Application</mainClass>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
  • 项目结构如下:

 

配置文件中指明文件访问的前后缀

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" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>${name},你好!</h2>
</body>
</html>

前端控制器代码:

/**
 * @author Dragon code!
 * @create 2022-05-02 15:15
 */
@Controller
public class MyController {
    @RequestMapping("/demo5")
    public String demo(Model model){
        model.addAttribute("name","汤世佳");
        return "myjsp";
    }
}

访问8080端口:可以看到成功整合jsp页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值