spring boot集成jsp
我们在使用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的更多相关文章
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
[原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...
- Spring Boot 集成Jsp与生产环境部署
一.简介 提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,因为目前市面上仍有很多的公司在使用JSP,所以本文就来介绍一下S ...
- spring boot 集成jsp
刚开始操作的时候,遇到了个问题,在这记录一下.(因为自己是个新手,对maven项目结构不了解) 1.大概创建步骤如下 File-New-Project-Spring Initializr ,type选 ...
- (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论: 您的认可是我最大的动力,感谢您的支持] 看完本文章您可能会有些疑问,可以查看之后的一篇博客: 81. Spring Boot集成JSP疑问[从 ...
- Spring Boot 集成Shiro和CAS
Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报 分类: Spring(42) 版 ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像。
背景:Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像. 原文地址 https://github.com/weibaohui/springboot ...
- Spring Boot集成Shrio实现权限管理
Spring Boot集成Shrio实现权限管理 项目地址:https://gitee.com/dsxiecn/spring-boot-shiro.git Apache Shiro是一个强大且 ...
- Spring Boot集成Jasypt安全框架
Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...
随机推荐
- supersocket为动态命令增加命令过滤器
由于我们无法像 C# 中一样方便的将 CLR 属性添加到 Python 文件或者函数中,因此我们需要定义一个函数 "getFilters()" 用于将命令过滤器方会给 CLR 运行 ...
- 2019-1-29-win10-uwp-使用-Microsoft.Graph-发送邮件
title author date CreateTime categories win10 uwp 使用 Microsoft.Graph 发送邮件 lindexi 2019-01-29 16:36:3 ...
- tf.nn.embedding_lookup()的用法
函数: tf.nn.embedding_lookup( params, ids, partition_strategy='mod', name=None, validate_indices=True, ...
- ssh使用笔记
在集群管理和配置中有很多命令要在各个节点中发送(特别是Master->Worker),大家都不希望发送每一个命令时都输入一次密码,因此常常先配置实现Master无密码登录到所有的Worker节点 ...
- 2019-8-6-在-Gitlab-开启-MatterMost-机器人
title author date CreateTime categories 在 Gitlab 开启 MatterMost 机器人 lindexi 2019-8-6 19:42:1 +0800 20 ...
- H3C STP基本配置
- 2019-8-31-win10-uwp-使用-WinDbg-调试
title author date CreateTime categories win10 uwp 使用 WinDbg 调试 lindexi 2019-08-31 10:30:35 +0800 201 ...
- 2019-3-1-C#-double-好用的扩展
title author date CreateTime categories C# double 好用的扩展 lindexi 2019-3-1 9:19:5 +0800 2018-05-15 10: ...
- [经典SQL语句]根据父级ID查找所有子级ID,并将所有ID用逗号隔开返回
树形表结构: id parentID isDel 1 0 0 2 1 0 3 1 1 4 2 0 5 2 0 一)根据父级ID查找所有子级ID,并将所有ID用逗号隔开返回 ID=,需要返回的结果(条件 ...
- CentOS遇到Qt编译问题(error: cannot find -lGL)
1.安装Qt,进入CentOS系统的终端,依次执行以下命令 chmod +x qt-opensource-Linux-x64-5.5.1.run ./qt-opensource-linux-x64-5 ...