提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
SpringBoot+MyBatis 调用jsp页面,前后端不分离的最终框架,前两天做了一个小工具没必要做前后端分离,所有自己写了jsp进行实现。
一、SpringBoot+MyBatis搭建
在之前写的文章中有讲过就不多数了直接上连接,自行搭建
https://blog.csdn.net/u013801673/article/details/108193775
二、搭建jsp环境
1.添加jsp依赖
pom.xml添加:
<!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2.maven配置
pom.xml 中 build 修改:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<configuration>
<mainClass>com.wisdom.Application</mainClass><!--入口类-->
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
<!-- 忽略无web.xml警告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包时将jsp文件拷贝到META-INF目录下 -->
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.application.yml配置
application.yml添加:
spring:
#试图控制器配置
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp
thymeleaf: #如果使用jsp跳转必须关闭默认模板引擎
cache: false
enabled: false
resources: #静态资源配置
static-locations: classpath:/webapp,classpath:/public,classpath:/resources,classpath:/META-INF/resources
3.jsp页面创建
在main文件夹下创建 webapp/WEB-INF 目录存放jsp页面
在main文件夹下创建 webapp/plugins 目录存放js、css 等静态资源
login.jsp:
<%--
Created by IntelliJ IDEA.
User: Tom
Date: 2020/8/24 0024
Time: 15:53
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>
${hello}
</body>
</html>
3.Controller 代码
package com.wisdom.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @Description:
* @Author: Tom
* @Date: 2020/8/24 0024 15:50
*/
@Controller
@RequestMapping("test")
public class TestController {
@Resource
private HttpServletRequest request;
@RequestMapping("login")
public String testRequstJSP(){
request.setAttribute("hello","helloWord!");
return "login";
}
}
配置到此结束,页面访问 http://127.0.0.1:10101/test/login