我是Spring引導(和servlet 3.0)的新手。我正在嘗試用JSP作爲視圖創建spring mvc項目。當我從我的控制器返回視圖時,它不會被解析爲JstlView。如何爲JSP配置spring boot mvc應用程序?
這裏是我做過什麼:
@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@Controller
public class MainController {
@RequestMapping(value="/main" , method = RequestMethod.GET )
public String main(){
return "main";
}
@RequestMapping(value="/" , method = RequestMethod.GET )
public String welcome(){
return "welcome";
}
}
同時創建的.jsp在src\main\webapp\WEB-INF\jsp
文件。
谷歌搜索後,我發現我需要在application.properties指定這個所以我添加以下道具:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework: TRACE
logging.level.com: TRACE
即使在此之後它無法正常工作。這是跟蹤。
2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking [MainController.welcome] method with arguments []
2016-04-24 19:54:49.016 TRACE 7880 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [welcome] returned [welcome]
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2016-04-24 19:54:49.020 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'welcome'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name 'welcome'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.s.v.InternalResourceViewResolver : Cached view [welcome]
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] based on requested media type 'text/html'
2016-04-24 19:54:49.022 DEBUG 7880 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'welcome'; URL [/WEB-INF/jsp/welcome.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2016-04-24 19:54:49.022 TRACE 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Rendering view with name 'welcome' with model {} and static attributes {}
2016-04-24 19:54:49.026 DEBUG 7880 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [/WEB-INF/jsp/welcome.jsp] in InternalResourceView 'welcome'
2
正如你在跟蹤中看到,這是試圖解決/jsp/welcome.jsp作爲InternalResourceView而不是JstlView。最終失敗爲404。
我需要遵循哪些其他步驟? SpringBoot-mvc有jsp教程嗎?
P.S.我可以使用web.xml(使用JstlView在我的配置文件中)使用jsp創建spring mvc應用程序。但是我找不到有關jsp的Spring啓動mvc的任何教程。
,看看到[此SO帖子](http://stackoverflow.com/questions/29953245/configure-viewresolver-with-spring-boot-and-annotations-gives-no-mapping-found-f)。可能有幫助。我們確實需要一個'InternalResourceViewResolver' bean來解決視圖。您需要與Spring Boot應用程序中的相同內容。 –
Spring Boot有一個[JSP示例應用程序](https://github.com/spring-projects/spring-boot/tree/v1.3.3.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp )在這裏可能會有所幫助 –
嘗試使用官方應用程序構建器http://start.spring.io來配置您的應用程序(默認情況下它使用JSP模板引擎)。 –