Reputation: 1849
This is a new issue that derived from a previous question I had. I am writing a Spring Boot Application which is using a .jsp as the mvc view. My folder structure is:
My application.properties is as follows:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.mvc.view.prefix= /WEB-INF/view/
spring.mvc.view.suffix= .jsp
Finally, my HomeController.java is as follows:
@Controller
@Slf4j
public class HomeController {
@RequestMapping(value="/", method= RequestMethod.GET)
public String showPage()
{
return "main-menu";
}
}
The program should render the main-menu.jsp file on my home page at localhost:8080, but instead I get an error. Here is the stack trace for the error:
2021-06-28 20:40:12.243 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={}
2021-06-28 20:40:12.245 DEBUG 27388 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.springdemo.mvc.HomeController#showPage()
2021-06-28 20:40:12.257 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-06-28 20:40:12.257 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : View name 'main-menu', model {}
2021-06-28 20:40:12.258 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to [/WEB-INF/view/main-menu.jsp]
2021-06-28 20:40:12.261 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/WEB-INF/view/main-menu.jsp", parameters={}
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2021-06-28 20:40:12.263 WARN 27388 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/view/main-menu.jsp]
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
2021-06-28 20:40:12.264 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2021-06-28 20:40:12.265 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2021-06-28 20:40:12.266 DEBUG 27388 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-06-28 20:40:12.278 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-06-28 20:40:12.281 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
As far as I can tell, it seems to be an issue with either the way IntelliJ is setup or the way Gradle is setup. Either way, the Resource directory or file is not being recognized.
Here's some of the things I've done/tried:
Thank you for any help I can get.
Edit:
I've updated my project structure as follows.
I also added apply plugin: 'war'
to my gradle.build and added the folder to my project facets.
I am still getting the same issue with resource not found.
Upvotes: 2
Views: 2517
Reputation: 1849
I was able to figure it out with the help of a coworker. I had to make a few changes to my SpringMvcDemoApplication.java file and to my gradle.build. Here are the changes.
SpringMvcDemoApplication (added bean for InternalResourceViewResolver):
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class SpringMvcDemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringMvcDemoApplication.class);
}
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
public static void main(String[] args) {
SpringApplication.run(SpringMvcDemoApplication.class, args);
}
}
HomeController.java (Removed @Slf4j)
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showPage()
{
return "main-menu";
}
}
gradle.build (added tomcat and jstl dependencies):
plugins {
id 'org.springframework.boot' version '2.5.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example.springdemo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
apply plugin: 'war'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.18'
implementation group: 'javax.servlet', name: 'jstl', version: '1.2' //addee
implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Everything works fine now.
Upvotes: 2
Reputation: 34
Following through from last question https://stackoverflow.com/a/68158449/14967681 please try changing your project structure as mentioned by bringing out WEB-INF out from resources into new folder as webapp. I am not sure of the reason why it fails to detect folder inside resources for that configuration mentioned but this should work for you. Here is the project structure you should use: https://i.sstatic.net/IZXx1.png
Upvotes: 0