How SpringBoot integrates web resources, LOOK

Create web project

How SpringBoot integrates web resources, LOOK

Edit project directory structure

How SpringBoot integrates web resources, LOOK

Import the jar package

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jt</groupId>
    <artifactId>springboot_demo3_web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>springboot_demo3_web</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--引入插件lombok 自动的set/get/构造方法插件  -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--引入数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--springBoot数据库连接  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--spring整合mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!--springBoot整合JSP添加依赖  -->
        <!--servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!--jstl依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--使jsp页面生效 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Edit YML files

server:
  port: 8090    #定义端口
  servlet:
    context-path: /   #定义项目的发布路径
spring:
  datasource:
    #driver-class-name: com.mysql.cj.jdbc.Driver   springboot程序采用默认的配置
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

  mvc:         #引入mvn配置
    view:
      prefix: /WEB-INF/     # /默认代表根目录 src/main/webapp
      suffix: .jsp

mybatis-plus:
  #定义别名包
  type-aliases-package: com.jt.pojo
  #加载user标签的mapper文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

#引入日志信息.
logging:
  level:
    com.jt.mapper: debug

Query user list data

【Claim】

The user requests via http://localhost:8090/findAll. Jump to the userList.jsp page, and display all the data in the user table, query by MP

Edit UserController

package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController   //json  字符串本身   不经过视图解析器
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 需求:用户通过http://localhost:8090/findAll
     * 跳转页面路径:userList.jsp
     * 页面取值信息: el表达式:${userList} 从域中取值.
     *              在页面跳转之前应该将userList数据保存到域中 key就是userList.
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){  //利用model对象将数据保存到request对象中.

        //1.查询数据库 获取list集合信息
        List<User> userList = userService.findAll();
        model.addAttribute("userList",userList);
        System.out.println(userList);
        return "userList";
    }

}

Edit UserService

package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {

        return userMapper.selectList(null);
    }
}

About IDEA startup web project configuration

Note: Configure IDEA startup items and configure the working directory to this project!

How SpringBoot integrates web resources, LOOK

Asynchronous way to achieve list display

Jump to the ajax page

Requirement: users pass: http://localhost:8090/ajax.jsp in the page

/**
     * 跳转到ajax.jsp页面
     */
    @RequestMapping("/ajax")
    public String ajax(){

        return "ajax";
    }

Page rendering

How SpringBoot integrates web resources, LOOK

Download jQuery function library

How SpringBoot integrates web resources, LOOK

Import function library

Note: just import the downloaded class library

How SpringBoot integrates web resources, LOOK

Hot deployment configuration

Add dependency

<!--支持热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

Configure automatic loading (open IDAE using shortcut key: ctrl+shift+alt+/)

How SpringBoot integrates web resources, LOOK

How SpringBoot integrates web resources, LOOK

Guess you like

Origin blog.csdn.net/qwe123147369/article/details/109249850