Spring Boot 本地部署 JSP

本文详细介绍了如何在SpringBoot项目中启用JSP支持,包括添加Tomcat-embed-jasper和javax.servlet-api依赖,配置SpringBootServletInitializer,以及设置视图解析器。作者分享了完整的工程结构和关键配置代码段。
摘要由CSDN通过智能技术生成

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

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

添加依赖

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

    <packaging>war</packaging>

配置主类

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

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 应该和网上的一致,我就简单记录一下。

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 {
}
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 };
    }

}

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";
    }

}
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();
//    }
}

附录

工程结构如下:
在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值