2016-04-25 23 views
5

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.

+0

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. –

+2

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 –

+0

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). –

Trả lời

3

Giả sử nó được nhúng Tomcat,

Bạn cần có sau trong pom.xml

<dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 

Embedded gói cốt lõi Tomcat của bạn đã không render hỗ trợ JSP.

10
@Bean 
public ViewResolver getViewResolver(){ 
    InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
    resolver.setPrefix("/WEB-INF/jsp/"); 
    resolver.setSuffix(".jsp"); 
    resolver.setViewClass(JstlView.class); 
    return resolver; 
} 

cũng cần thiết và trang của bạn nên có ít/webapp/WEB-INF/jsp/

+

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 
+0

Thanx. it's đã giúp tôi – vvator

+0

Đã không hoạt động đối với tôi, khởi động mùa xuân 1.4.3-RELEASE, cho đến khi tôi thêm viewResolver thì hoạt động –

2

Trước tiên, bạn cần thêm phụ thuộc maven thích hợp như dưới đây để làm cho JSP trong ứng dụng khởi động mùa xuân.

<dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tomcat.embed</groupId> 
     <artifactId>tomcat-embed-jasper</artifactId> 
    </dependency> 

Sau đó xác định các lớp xem như JstlView như sau:

@Bean 
public InternalResourceViewResolver setupViewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix ("/ui/jsp/"); 
     resolver.setSuffix (".jsp"); 
     resolver.setViewClass (JstlView.class); 
     return resolver; 
    } 

Tìm một ví dụ làm việc ở đây spring boot mvc jsp integration example

1

Bạn không yêu cầu ViewResolver. pom.xml cần các phụ thuộc được đề cập như được Yura nói và đặt các tệp jsp trong src \ main \ webapp \ WEB-INF \ jsp.

-1
  1. Tạo webapp/WEB-INF/views {Đặt tên cho thư mục cuối cùng như U Giống như} dưới src/main
  2. thêm JSTL jar

  3. add sau hai dòng trong application.properties

    spring.mvc.view.prefix:/WEB-INF/views/ spring.mvc.view.suffix: .jsp

    Run As xuân Boot App ..U là tốt để đi!

    Để biết thêm U có thể tham khảo dự án này của tôi trên github: https://github.com/SudipBiswasRana/Using-JSP-As-View-For-Spring-Project

Các vấn đề liên quan