新冠病毒还在阻挡全世界重启,但我们学习脚步不不能停滞,接下来给大家展示一个现在开发中已经不太常用的一个小知识点,希望对大家有所启发。
在平时 大家可能用 Spring Boot 2 最多就是开发 RESTful API,可能很少有人在 Spring Boot 2 中用过JSP视图,那我就来一起体验下创建一个用 JSP 视图的 Spring Boot 2 应用有多么方便。

一起来看看我们需要些什么

项目结构

咱们可以从 Spring Initializer 获取项目框架。

项目依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eprogrammerz.examples.spring</groupId>
<artifactId>spring-boot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jsp</name>
<description>Example Spring Boot with JSP view</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

配置

  1. 启动类配置

SpringBootServletInitializer 按传统的 WAR包 部署方式来运行 SpringBootJspApplication
SpringBootJspApplication.java

package com.eprogrammerz.examples.spring.springbootjsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootJspApplication.class, args);
}
}
  1. Resources
    application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Controller and View Template

  1. 写一个简单映射方法的Controller
package com.eprogrammerz.examples.spring.springbootjsp.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping({"/", "/hello"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
}
  1. 将下面内容保存成 JSP 文件,放在src/main/webapp/WEB-INF/jsp/目录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello ${name}!</title>
</head>
<body>
<h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>

使用Maven运行

在项目根路径下使用命令行运行程序。

mvn clean spring-boot:run

访问 localhost:8080 测试你的程序效果。

至此,使用 Spring Boot 2 展示 JSP 页面基础配置就完成,希望对大家有一定帮助。

提前️大家新年新气象,2021年技术更上一个新台阶!

本文作者: 浩子淘天下
本文链接: http://blog.chuangzhi8.cn/posts/11-spring-boot-2-with-jsp-view.html
版权声明: 本文由 窗纸儿吧-浩子淘天下 创作,采用 CC BY-NC-SA 3.0协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加下图 作者公众号二维码

最新文章

  1. 关于webrtc视频会议的解决方案
  2. MyCat 学习笔记 第十二篇.数据分片 之 分片事务处理
  3. 想直接用Windows API开发程序,你真的应该知道这些!
  4. LCIS(m*n) 最长公共上升子序列
  5. Git教程之多人协作
  6. Android 签名(6)编译时源码的签名
  7. 洛谷 P3367 【模板】并查集
  8. 【小分享】Date对象封装,时间格式化函数time()
  9. RESTful 最佳实战
  10. Spring之旅第一篇-初识Spring
  11. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]
  12. Confluence 6 配置字符集编码
  13. 生成Area URL链接
  14. stl string的erase方法
  15. Delphi7使用一段时间后抽风提示注册
  16. [leetcode]34.Find First and Last Position of Element in Sorted Array找区间
  17. 安装搭建Python2.* 和3.* 环境详细步骤
  18. 二叉树的递归,非递归遍历(C++)
  19. 对象的继承(__proto__和Object.setPrototypeOf(child,father))
  20. Neuron network

热门文章

  1. LeetCode454. 四数相加 II
  2. SDUST数据结构 - chap5 数组与广义表
  3. Flask+pin
  4. 环境变量IFS
  5. [Usaco2016 Dec]Counting Haybales
  6. MVC和MTV框架模式
  7. 牛逼!MySQL 8.0 中的索引可以隐藏了…
  8. Python+Selenium+Unittest实现PO模式web自动化框架(1)
  9. 在Golang中如何正确地使用database/sql包访问数据库
  10. (007)每日SQL学习:将字符和数字分离