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 ...
随机推荐
- Java多线程遍历文件夹,广度遍历加多线程加深度遍历结合
复习IO操作,突然想写一个小工具,统计一下电脑里面的Java代码量还有注释率,最开始随手写了一个递归算法,遍历文件夹,比较简单,而且代码层次清晰,相对易于理解,代码如下:(完整代码贴在最后面,前面是功 ...
- Python 数据类型,常用函数方法分类
Python基本数据类型:(int) 字符串(str)列表(list)元组(tuple)字典(dict)布尔(bool) python中可以简单使用 类型(数据)创建或转换数据 例: #字符串转数字 ...
- H3C 端口接入控制方式
- vue element UI el-table 表格调整行高的处理方法
这是我在工作项目中遇到的问题,我想将标记处下方的表格高度调低一点,也就是想实现下面的这个效果: 代码调整如下: 说明: 缩小:行高到一定程度之后便不能缩小. 好像最小35px.各位可以试一下. 升高: ...
- Java 趣事之 a=a++ 和 a=++a(转)
转自:https://blog.csdn.net/LovePluto/article/details/81062176 如果问 a++ 和 ++a 的区别,估计很多都能回答上来.a++ 是先取 a 的 ...
- 【t068】智慧碑
Time Limit: 1 second Memory Limit: 128 MB [问题描述] DIABLO魔王和Mini都有三种属性,体力点,攻击点,以及集气点. 两人的攻击方式是这样的:采用回合 ...
- jQuery 工具类函数-浏览器信息
在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息,如$.browser.chrome为true,表示当前为Chrome浏览器,$.browser.mozilla为true,表 ...
- js实现new
function New(fn,...args){ let obj={} obj.__proto__=fn.prototype let result=fn.apply(obj,args) if(typ ...
- Windows 服务安装与卸载 (通过 Sc.exe)
1. 安装 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: sc create "Verity Platform De ...
- 魅族--魅蓝metal
评论:金属潮流平民化