Tôi mới khởi động Spring (và servlet 3.0). Tôi đang cố gắng để tạo ra dự án mvc mùa xuân với JSP như xem. Khi tôi trở về một cái nhìn từ bộ điều khiển của tôi nó không được giải quyết như JstlView.Làm thế nào để cấu hình ứng dụng mvc khởi động mùa xuân cho JSP?
Dưới đây là những gì tôi đã làm:
@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";
}
}
Created cả file .jsp trong src\main\webapp\WEB-INF\jsp
.
Sau khi googling tôi thấy rằng tôi cần phải xác định này trong application.properties Vì vậy, tôi thêm vào sau trong đạo cụ:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework: TRACE
logging.level.com: TRACE
Ngay cả sau này nó không được làm việc. Đây là dấu vết.
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
Như bạn thấy trong theo dõi, điều này đang cố gắng giải quyết /jsp/welcome.jsp dưới dạng InternalResourceView thay vì JstlView. Cuối cùng nó không thành công như 404.
Tôi cần làm theo các bước nào khác? Có bất kỳ hướng dẫn cho SpringBoot-mvc với jsp?
P.S. Tôi có thể tạo ứng dụng mvc mùa xuân với jsp bằng cách sử dụng web.xml (sử dụng JstlView trong tệp cấu hình của tôi). Nhưng tôi không thể tìm thấy bất kỳ hướng dẫn cho mvc khởi động mùa xuân với jsp.
Mặc dù tôi không quen thuộc với mùa xuân Boot, (tôi thích sự phát triển mùa xuân là "di sản" chiều), có một cái nhìn vào [này SO post] (http://stackoverflow.com/questions/29953245/configure-viewresolver-with-spring-boot-and-annotations-gives-no-mapping-found-f). Có thể giúp. Chúng ta cần một bean 'InternalResourceViewResolver' để các khung nhìn được giải quyết. Bạn cần tương đương với nó trong một ứng dụng khởi động mùa xuân. –
Khởi động mùa xuân có [ứng dụng mẫu JSP] (https://github.com/spring-projects/spring-boot/tree/v1.3.3.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp) có thể hữu ích ở đây –
Hãy thử sử dụng trình tạo ứng dụng chính thức http://start.spring.io để định cấu hình ứng dụng của bạn (theo mặc định nó sử dụng công cụ mẫu JSP). –