java - gradle + spring MVC + spring boot + jsp = 404错误?

标签 java spring jsp spring-mvc spring-boot

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

我有一个 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'

第一条消息是由我的 Controller 生成的:

    @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

所以 - 我的 Controller 被调用,它正在打印它正在分派(dispatch)到的 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

最佳答案

而不是

return "index"

作为一个字符串,你可以尝试下面的东西

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

关于java - gradle + spring MVC + spring boot + jsp = 404错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307914/

相关文章:

java - 从 JSP 中的属性目录获取文本文件

eclipse - Java Servlet 和 JSP/JSF 帮助/指南

java - 下载图像时 FileOutputStream 崩溃并出现 "open failed: EISDIR (Is a directory)"错误

java - 用于距离函数的最佳 Java API

spring - 使用 PageableDefault 测试剩余 Controller

java - 在加载 index.jsp 之前调用 Controller

java - 从 JSP 文件更新 HTML

java - 迷宫算法的数组未存储正确的值

java - Apache CXF 网络服务问题

java - Spring : Use Hibernate types without hibernate. cgf.xml