在 Spring Boot 2 中致敬 JSP

新冠病毒🦠还在阻挡全世界重启,但我们学习脚步不不能停滞,接下来给大家展示一个现在开发中已经不太常用的一个小知识点,希望对大家有所启发。
在平时 大家可能用 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot,可以使用EnvironmentPostProcessor来修改应用程序的环境。要使用EnvironmentPostProcessor,需要遵循以下步骤: 1. 创建一个类并实现EnvironmentPostProcessor接口。 2. 在META-INF/spring.factories文件注册EnvironmentPostProcessor实现类。 3. 在实现类重写postProcessEnvironment方法,并在其修改环境。 下面是一个简单的示例,演示如何在Spring Boot使用EnvironmentPostProcessor: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { // 在这里修改环境 environment.getPropertySources().addLast(new MyPropertySource()); } } ``` 在上面的示例,我们创建了一个名为MyEnvironmentPostProcessor的类,并实现了EnvironmentPostProcessor接口。在postProcessEnvironment方法,我们可以修改环境。在这个例子,我们添加了一个自定义的属性源。 要注册EnvironmentPostProcessor实现类,需要在META-INF/spring.factories文件添加以下内容: ``` org.springframework.boot.env.EnvironmentPostProcessor=com.example.MyEnvironmentPostProcessor ``` 这将告诉Spring Boot在启动时加载MyEnvironmentPostProcessor类,并调用postProcessEnvironment方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩子淘天下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值