Spring Boot 本地部署 JSP

自己是Spring Boot 的初学者,开始看教程的时候发现基本上都是部署的 JSP,但是按照教程一步步走下来始终无法成功,一直都是 404:

查阅各种资料后,总结出一套 Spring Boot 支持 JSP 的流程:

添加依赖

pom.xml中添加两个依赖:tomcat-embed-jasper(它提供了JSP解析和执行的功能)和 javax.servlet-api(它提供了对 JSP 所需的 Servlet API 的支持)。这是所需的 pom.xml内容:

xml 复制代码
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

要注意的是,将 pom.xml 文件的 packing 方式修改为 war:

xml 复制代码
    <packaging>war</packaging>

配置主类

配置 SpringBootServletInitializer 子类以启用 JSP 支持。

java 复制代码
package com.example.springfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.wavefront.WavefrontProperties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringFirstApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WavefrontProperties.Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringFirstApplication.class, args);
    }

}

常规内容

其它的配置,比如 mapping 应该和网上的一致,我就简单记录一下。

java 复制代码
package com.example.springfirst.spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages={"com.example.springfirst.spittr"})
public class RootConfig {
}
java 复制代码
package com.example.springfirst.spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.example.springfirst.spittr.web.WebConfig;

public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

}
java 复制代码
package com.example.springfirst.spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;

@Controller
public class HomeController {

    @PostConstruct
    public void init() {
        System.out.println("HomeController bean created");
    }

    @RequestMapping(value="/home", method = GET)
    public String home() {
        return "home";
    }

}
java 复制代码
package com.example.springfirst.spittr.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.example.springfirst.spittr.web")
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }
}

附录

工程结构如下:

相关推荐
2401_857439691 小时前
Spring Boot新闻推荐系统:用户体验优化
spring boot·后端·ux
hong_zc1 小时前
算法【Java】—— 二叉树的深搜
java·算法
进击的女IT2 小时前
SpringBoot上传图片实现本地存储以及实现直接上传阿里云OSS
java·spring boot·后端
Miqiuha2 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
杨半仙儿还未成仙儿3 小时前
Spring框架:Spring Core、Spring AOP、Spring MVC、Spring Boot、Spring Cloud等组件的基本原理及使用
spring boot·spring·mvc
一 乐3 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
数云界3 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
阑梦清川3 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
快乐就好ya5 小时前
Java多线程
java·开发语言
IT学长编程5 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统