栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合JSP,SSM

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合JSP,SSM

1. SpringBoot整合JSP SpringBoot 应⽤默认⽀持的动态⽹⻚技术是 Thymeleaf ,并不⽀持 JSP ;因此在 SpringBoot 应⽤想要使⽤ JSP 需要通过⼿动整合来实现 1.1 添加依赖

    org.apache.tomcat.embed
    tomcat-embed-jasper
    9.0.45


    javax.servlet
    jstl
    1.2
1.2 创建jsp页面 1.2.1 修改pom文件打包方式为war
war
1.2.2 在main中创建webapp目录,在webapp下创建WEB-INF

 

1.2.3 在webapp创建.jsp页面

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


index..

1.3 将jsp页面放在WEB-INF中访问 出现问题

出现错误: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

分析:webapp目录下缺少WEB-INF/web.xml

当是在servlet 3.0之后,对于web.xml文件本身是可选的

解决:1.在pom.xml文件中定义一个参数配置 


        1.8
        UTF-8
        UTF-8
        2.4.1
        
        false
    

2.在webapp目录下创建WEB-INF/web.xml

 

1.3.1 将jsp文件存放到WEB-INF目录运行
http://localhost:8080/index.jsp

 

1.3.2 在application.yml文件配置SpringMVC视图解析方法
# 支持 yaml 语法配置
server:
  port: 9999
  servlet:
    context-path: /demo1

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/maven_demo1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
#    视图解析器的配置
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp

mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml
  type-aliases-package: com.qfedu.springboot.demo.entity
创建PageController
package com.qfedu.springboot.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class PageController {

    @RequestMapping("/index.html")
    public String index(){
        return "index";
    }
}
2. 基于SpringBoot的SSM整合 2.1 创建SpringBoot项目         创建项目时添加依赖

lombok,spring,mysql driver,mybatis framework 

 进行mybatis所需要的配置

配置 aplication.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/maven_demo1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: 123456
mybatis:
  type-aliases-package: com.qfedu.springboot.ssm.beans
  mapper-locations: classpath:mappers/*.xml
在启动类配置dao扫描,配置SpringbootSsmApplication,如果多个dao,@MapperScan({"",""})

 整合Druid连接池 在 SpringBoot 中整合 MyBatis 的时候,默认集成了 Hikari 连接池, Hikari 的效率⽐ Druid 要⾼,但是得益于 Druid 提供了⽐较便捷的监控系统在企业开发中。 添加druid的starter

            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
配置druid数据源 在application.yam中配置
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/maven_demo1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
      username: root
      password: 123456
      initial-size: 1
      min-idle: 1
      max-active: 20
mybatis:
  type-aliases-package: com.qfedu.springboot.ssm.beans
  mapper-locations: classpath:mappers/*.xml

验证是否配置成功

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877837.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号