Java 格拉德尔+;spring MVC+;弹簧靴&x2B;jsp=404错误?
我在Java 格拉德尔+;spring MVC+;弹簧靴&x2B;jsp=404错误?,java,spring,jsp,spring-mvc,spring-boot,Java,Spring,Jsp,Spring Mvc,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.add
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'
第一条消息由我的控制器生成:
@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目录复制到正在运行的webapp的pwd的内容?使用Maven-webapp目录被放入target/
中,因此它位于类路径上。少了什么
编辑
我向build.gradle
文件添加了以下依赖项以解决此问题:
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
compile("javax.servlet:jstl")
另请参见:,而不是
return "index"
作为一个字符串,你能试试下面这样的东西吗
@RequestMapping({"/", "/index"})
public ModelandView index(){
LOGGER.info("Running index from MVC, returning index view");
return new ModelandView("index");
}
我已经试过了,更不用说在这方面它们都是一样的。看这里,你解决问题了吗?您使用的是哪种gradle版本?