新冠病毒????还在阻挡全世界重启,但咱们学习脚步不不能停滞,接下来给大家展现一个当初开发中曾经不太罕用的一个小知识点,心愿对大家有所启发。
在平时 大家可能用 Spring Boot 2 最多就是开发 RESTful API,可能很少有人在 Spring Boot 2 中用过JSP视图,那我就来一起体验下创立一个用 JSP 视图的 Spring Boot 2 利用有如许不便。

一起来看看咱们须要些什么

我的项目构造

咱们能够从 Spring Initializer 获取我的项目框架。

我的项目依赖

<u>pom.xml</u>

<?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
<u>SpringBootJspApplication.java</u>

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;@SpringBootApplicationpublic 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

<u>application.properties</u>

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;@Controllerpublic 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协定 进行许可。 可自在转载、援用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末增加下图 作者公众号二维码