关于springboot:京淘day03SpringBoot整合web

6次阅读

共计 4516 个字符,预计需要花费 12 分钟才能阅读完成。

1.SpringBoot 整合 web 资源

1.1 创立动静 web 资源

1.2 我的项目构造

1.3 增加资源 /jar 包

1). 增加资源

2). 增加 jar 包文件

 <!--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>

1.4 增加 YML 配置文件

server:
  port: 8090
  servlet:
    context-path: /     #我的项目根目录公布
spring:
  datasource:
    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

# Spring 整合 Mybatis-plus 配置
mybatis-plus:
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

# 打印 Mybatissql 语句
logging:
  level:
    com.jt.mapper: debug

1.5 入门案例

需要: 用户通过 http://localhost:8090/findAll 获取全副 userList 汇合, 并且在 userList.jsp 页面中进行表格数据展示

1.5.2 动静 web 资源 404 报错阐明

阐明:IDEA 默认条件下工作目录抉择不正确的, 须要手动配置一下, 留神工作目录编辑

1.5.2 编辑 UserController

编码方式 pojo->Controller->Service->Mapper 自上而下的开发方式

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.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model){List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "userList";
    }
}

1.5.3 编辑 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);
    }
}

1.5.4 页面成果展示

1.6 异步实现业务调用

1.6.1 Ajax 为什么能够异步呢? 工作原理是?

阐明:
1).Ajax 两头有 ajax 引擎的参加.ajax 引擎本质就是一种代理的思维
2). 因为须要实现异步的操作, 所以申请必然是屡次申请. 屡次响应.

1.6.2 导入 jQuery.JS

1.6.3 Ajax 申请流程

如果须要发动 Ajax 申请时, 个别须要发动 2 个申请.
1 个是用来跳转页面的 http://localhost:8090/toAjax
1 个是用来申请数据的 http://localhost:8090/findAjax

1.6.4 编辑页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <title> 您好 Springboot</title>
  <!-- 引入函数类库 -->
 <script src="../js/jquery-3.4.1.min.js"></script>
 <script type="text/javascript">
      
        //1. 让页面全副加载实现  函数式编程
          $(function(){//1.$.get(url 地址, 传递的参数, 回调函数, 返回值类型)  
        //$.post()   $.getJSON  $.ajax
          $.ajax({
          url:"/findAjax",          //url 地址
          method:"get",             // 参数类型
          data:{id:1,name:"tomcat"},// 申请参数
          success: function(data){//console.log(data);
          for(let user of data){
          let id = user.id;
          let name = user.name;
          let age = user.age;
          let sex = user.sex;
          let tr="<tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td></tr>"
          $("#table1").append(tr);
           }
          },
          error: function(data){alert("申请失败");
          },
          cache: true,// 缓存默认 true
          async: true,// 同步异步默认异步 true
         })
         // 对于参数写法 2 种 
         //1.JSON 格局 {id:1,name:"tomcat"}   
         //2. 字符串拼接 id=1&name="tomcat" 
         // $.get("/findAjax",{id:1},function(data){//console.log(data);    
         // 循环遍历形式 3  of
         //for(let user of data){
         //let id = user.id; 
         //let name = user.name; 
         //let age = user.age; 
         //let sex = user.sex; 
         //let tr="<tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td></tr>"          //$("#table1").append(tr); 
         // } 
         // 循环遍历形式 2  in
         //for(let index in data){//let user=data[index]; 
         //let id=user.id; 
         //let name=user.name; 
         //let age=user.age; 
         //let sex=user.sex; 
         //console.log(user); 
         //} 
         // 循环遍历形式 1 for
         //for(let i=0;i<data.length;i++){//let user=data[i]; 
         //let id=user.id; 
         //let name=user.name; 
         //let age=user.age; 
         //let sex=user.sex; 
         //console.log(id+":"+name+":"+age+":"+sex);
         //} 
         //})
         })
      </script>
      </head>
      <body>
         <table id="table1" border="1px" width="65%" align="center">
            <tr>
               <td colspan="6" align="center"><h3> 学生信息 </h3></td>
            </tr>
            <tr>
               <th> 编号 </th>
               <th> 姓名 </th>
               <th> 年龄 </th>
               <th> 性别 </th>
            </tr>
         </table>
      </body>
      </html>

1.6.5 编辑 UserController

    // 跳转到 ajax.jsp 页面中
    @RequestMapping("toAjax")
    public String toAjax(){return "ajax";}
    /**
     * 接管 ajax 申请: /findAjax
     * 返回值:  List<User>
     */
    @RequestMapping("/findAjax")
    @ResponseBody //1. 将返回值后果转化为 JSON 数据返回   2. 代表 ajax 申请完结
    public List<User> findAjax(){return userService.findAll();
    }
正文完
 0