我们在使用spring boot进行web项目开发的时候,可能会选择页面用jsp。spring boot默认使用的html的,现在我们来看下如何集成jsp页面进行开发。

1.pom.xml文件引入所需依赖

我们要在pom.xml文件中引入jsp页面所需要的jar包,如下:

<!-- springboot支持jsp -->
<!--引入springboot内嵌的tomcat对jsp的解析包-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- servlet依赖的jar包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- jsp依赖的jar包 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- jstl标签依赖的jar包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

2.配置文件设置前端视图展示为jsp

在application.properties文件中配置前端视图页面展示为jsp页面,

#前端视图展示jsp页面
#springmvc的前缀,页面的所在位置
spring.mvc.view.prefix=/
#springmvc的后缀
spring.mvc.view.suffix=.jsp

3.编写一个jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body> ${msg}
</body>
</html>

我们写一个简单的jsp页面,里面body中打印了一个变量msg。

4.编写一个访问控制器

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class JspController { @GetMapping("/index")
public String index(Model model){ model.addAttribute("msg","spring boot集成jsp");
return "index";
}
}

在这里,应为需要返回页面,所以我们用的注解是@Controller。@RestController注解是返回json格式的字符串。相当于@Controller + @ResponseBody。然后启动项目,访问地址http://127.0.0.1:8088/demo/index,效果如下:

这里我们需要注意下,如果访问的时候,获取不到jsp页面,那么我们需要在pom.xml文件进行修改编译jsp页面后的位置。在build标签下增加resources部分,如下:

    <resources>
<!-- 将xml也编译 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource> <!-- src/main/webapp目录下所有的文件编译到 META-INF/resource下 -->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resource</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>

spring boot集成jsp的更多相关文章

  1. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  2. Spring Boot 集成Jsp与生产环境部署

    一.简介 提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,因为目前市面上仍有很多的公司在使用JSP,所以本文就来介绍一下S ...

  3. spring boot 集成jsp

    刚开始操作的时候,遇到了个问题,在这记录一下.(因为自己是个新手,对maven项目结构不了解) 1.大概创建步骤如下 File-New-Project-Spring Initializr ,type选 ...

  4. (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...

  5. Spring Boot 集成Shiro和CAS

    Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报  分类: Spring(42)  版 ...

  6. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  7. Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像。

    背景:Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像. 原文地址 https://github.com/weibaohui/springboot ...

  8. Spring Boot集成Shrio实现权限管理

    Spring Boot集成Shrio实现权限管理   项目地址:https://gitee.com/dsxiecn/spring-boot-shiro.git   Apache Shiro是一个强大且 ...

  9. Spring Boot集成Jasypt安全框架

    Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...

随机推荐

  1. jQuery仿迅雷图片轮换效果

    jQuery仿迅雷图片轮换效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  2. 基于LIVE555的RTSP QoS实现

    如何从OnDemandServerMediaSubsession类以及继承类对象中获取RTCP信息(句柄) OnDemandServerMediaSubsession.cpp void StreamS ...

  3. 将 vue.js 获取的 html 文本转化为纯文本

    我存入数据表中的数据是使用 html  格式,获取数据是使用 vue 获取. 遇到了一个问题,就是界面上显示的数据是 html 格式的,但是我需要它显示纯文本. 怎么做呢?首先在  js  中写一个将 ...

  4. .net core 控制台下使用HttpClientFactory封装

    HttpClientFactory封装,如有错误请指出,谢谢! using System; using System.Collections.Generic; using System.Net.Htt ...

  5. 我来教你用AWS IoT.Part1--配置和接入

    AWS的IOT服务在中国区才开放.由于工作原因需要简单试用评估.写一下自己简单试用的流程,供其他人参考. 直接贴流程 1.先注册一个类型(这里“类型”相对于编程,可以理解为父类,里面可以添加一些可继承 ...

  6. P1005 等边字符三角形

    题目描述 给定一个字符串,用它构造一个底边长5个字符,高3个字符的等腰字符三角形. 三角形的形状见样例输出. 输入格式 无. 输出格式 输出样例输出中所描述的等腰字符三角形. 样例输入 无. 样例输出 ...

  7. 喵喵电影git操作

    1.git remote 2.git remote add origin '项目地址'   (origin为远程仓库名字) 3.git remote 4.git push origin master ...

  8. 【30.01%】【hdu 3397】Sequence operation

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  9. Linux 内核bin+attribute 结构二进制属性

    sysfs 惯例调用所有属性来包含一个单个的人可读文本格式的值. 就是说, 只是偶然地很 少需要来创建能够处理大量二进制数据的属性. 这个需要真正地只出现在必须传递数据, 不可动地, 在用户空间和设备 ...

  10. CodeForces - 1162E Thanos Nim (博弈论)

    Alice and Bob are playing a game with nn piles of stones. It is guaranteed that nn is an even number ...