【问题标题】:gradle + spring MVC + spring boot + jsp = 404 error?gradle + spring MVC + spring boot + jsp = 404错误?
【发布时间】:2018-06-26 17:39:29
【问题描述】:

我在src/main/webapp/WEB-INF/view/index.jsp 中有一个带有 jsp 的 Spring Boot 应用程序

我有一个 MVC 配置:

@Configuration
public class MSMVCConfigurtionBeans extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/res/**").addResourceLocations("/static/");
    }

    @Bean()
    DispatcherServlet dispatcherServlet() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        return dispatcherServlet;
    }

    @Bean()
    ServletRegistrationBean dispatcherServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/");
        registration.setName("MVCDispatcher");
        return registration;
    }

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

我运行gradle bootRun 并加载索引页面并在日志中看到:

2018-01-17 13:36:17.623  INFO 50410 --- [nio-8080-exec-1] com.s.IndexController     : Running index from MVC, returning index view
2018-01-17 13:36:17.625 DEBUG 50410 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'index'
2018-01-17 13:36:17.636 DEBUG 50410 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/view/index.jsp] in InternalResourceView 'index'

第一条消息由我的控制器生成:

    @RequestMapping({"/", "/index"})
    public String index(){
        LOGGER.info("Running index from MVC, returning index view");
        return "index";
    }

这是我的 gradle 构建文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: "jacoco"
apply plugin: 'war'

jar {
    baseName = 'i-rest'
    version = '0.1.1'
}

repositories {
    mavenCentral()
    maven {
        url "https://dl.bintray.com/michaelklishin/maven/"
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-validation")
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
    compile("org.springframework.boot:spring-boot-starter-security")

    compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.2.Final'
    compile("org.springframework:spring-tx")
    compile 'org.springframework.security:spring-security-web'
    compile("org.springframework:spring-context-support")
    compile("org.quartz-scheduler:quartz:2.2.1")
    compile("org.projectlombok:lombok")
    compile("com.novemberain:quartz-mongodb:2.0.0")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

ls -al src/main/webapp/WEB-INF/view/
total 8
drwxr-xr-x  3 mike  staff   96 Jan 16 19:16 .
drwxr-xr-x  3 mike  staff   96 Jan 17 13:22 ..
-rw-r--r--  1 mike  staff  751 Jan 15 13:38 index.jsp

所以 - 我有我的控制器被调用,它正在打印它正在调度的 jsp(它存在),为什么是 404?我是否在 gradle 中遗漏了一些没有将 WEB-INF 目录复制到正在运行的 web 应用程序的 pwd 的内容?使用 Maven - webapp 目录被放入target/,因此它位于类路径中。缺少什么?

编辑

我在build.gradle 文件中添加了以下依赖项来解决这个问题:

compile("org.apache.tomcat.embed:tomcat-embed-jasper")
compile("javax.servlet:jstl")

另请参阅:Spring Boot JSP 404

【问题讨论】:

  • 您的问题解决了吗?您使用的是哪个 gradle 版本?

标签: java spring jsp spring-mvc spring-boot


【解决方案1】:

代替

return "index"

作为一个字符串,你能试试下面这样的东西吗

@RequestMapping({"/", "/index"}) 
 public ModelandView index(){ 
     LOGGER.info("Running index from           MVC, returning index view"); 
    return new ModelandView("index"); 
 }

【讨论】:

猜你喜欢
  • 2015-08-10
  • 2017-10-06
  • 2014-12-05
  • 2012-08-03
  • 1970-01-01
  • 2015-02-03
  • 2016-11-19
  • 2017-03-06
  • 2018-12-24
相关资源
最近更新 更多