SpringBoot+MyBatis 调用jsp页面

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

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


总结

spring-boot-maven-plugin 的版本必须使用 1.4.2.RELEASE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值