我们在使用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. poj 1039 Pipe (Geometry)

    1039 -- Pipe 理解错题意一个晚上._(:з」∠)_ 题意很容易看懂,就是要求你求出从外面射进一根管子的射线,最远可以射到哪里. 正解的做法是,选择上点和下点各一个,然后对于每个折点位置竖直 ...

  2. phpstorm 中git 的使用

    phpstorm 是一款不可多得的IDE 环境,它既是一款强大的编辑器,还是一款调试器,同时还是一款git 可视化管理工具哟,phpers 值得拥有.今天介绍的是他不为人知的强大功能 —— git. ...

  3. Python数据可视化matplotlib和seaborn

    Python在数据科学中的地位,不仅仅是因为numpy, scipy, pandas, scikit-learn这些高效易用.接口统一的科学计算包,其强大的数据可视化工具也是重要组成部分.在Pytho ...

  4. js基础——面向对象(构造函数)

    1.面向对象:类的标志,通过类可创建多个具有相同属性和方法的对象 2.创建对象 1)工厂模式方式:避免重复实例化但未能解决识别问题  function boss(name, age) {       ...

  5. SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案

    SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...

  6. Canvas动画:地球绕着太阳转

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  7. vue-cli常用插件集合

    element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI ...

  8. dotnet 线程静态字段

    在 dotnet 程序提供了一个好用的特性,可以让字段作为线程的静态字段,也就是在相同线程的所有代码访问的静态字段是相同对象,但不同线程访问的时候是不同的 在 .NET 程序可以使用 ThreadSt ...

  9. 通过Servlet生成验证码图片(转)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  10. 三、解析class文件

    一.class文件 https://blog.csdn.net/tyyj90/article/details/78472986 https://blog.csdn.net/sinat_38259539 ...