2014-07-27 28 views
16

Tôi không thể nhận được mvc mùa xuân để giải quyết tệp xem .html.Spring MVC ViewResolver không ánh xạ tới các tệp HTML

tôi có cấu trúc xem thư mục sau:

WEB-INF 
     `-views 
      |- home.jsp 
      `- home.html 

Tôi có một phương pháp điều khiển hello world đơn giản rằng chỉ cần in một thông điệp và trả về tên xem "nhà". Tôi có một tệp home.jsp nhưng thay vào đó, hãy muốn sử dụng home.html.

<!-- Working servlet mapping --> 
<servlet-mapping> 
    <servlet-name>spaceShips</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- working servlet context --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

Khi tôi nhấn con tàu vũ trụ/home bộ điều khiển in thông điệp trên thế giới chào và tôi thấy quan điểm home.jsp mà không có một vấn đề.

Vấn đề là khi tôi thay đổi hậu tố thành .html.

Sau khi thay đổi hậu tố và điều hướng đến/home, bộ điều khiển in thông điệp tuy nhiên tôi thấy một lỗi 404 trong trình duyệt và sau trong giao diện điều khiển: CẢNH BÁO: Không có ánh xạ tìm thấy cho yêu cầu HTTP URI [/ tàu vũ trụ /WEB-INF/views/home.html]

Để làm rõ:

<!-- not working with .html --> 
<servlet-mapping> 
    <servlet-name>spaceShips</servlet-name> 
    <!-- I have tried /* here as well without success --> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- not working with .html--> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="WEB-INF/views/" /> 
    <beans:property name="suffix" value=".html" /> 
</beans:bean> 

tôi đã kiểm tra trong thư mục chiến tranh bùng nổ và có thể khẳng định rằng cả hai file nhà có mặt.

Có ai gặp phải điều gì đó như thế này trước đây không?

đoạn cuối của thông điệp giao diện điều khiển:

INFO: Server startup in 5256 ms 
Hello, World! 
Jul 27, 2014 12:52:01 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/spaceships/WEB-INF/views/home.html] in DispatcherServlet with name 'spaceShips' 

Cảm ơn bạn đã đọc.

=========== SOLUTION ============

Sau đây (xấu xí) cấu hình giải quyết vấn đề này. Có lẽ có nhiều cách để làm sạch điều này, nhưng nếu bạn đang gặp vấn đề tương tự, bạn có thể kết hợp với nhau một giải pháp từ việc này.

cấu trúc

Folder:

WEB-INF 
     `-static 
       |-html 
        `-home.html 
       |-css 
       `-img 

điều khiển phương pháp:

@RequestMapping(value = "/home") 
public String goHome() { 
     System.out.println("lolololololol"); 
     return "static/html/home"; 
} 

mùa xuân config:

<resources mapping="/static/**" location="/WEB-INF/static/" /> 

<beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="" /> 
     <beans:property name="suffix" value=".html" /> 
</beans:bean> 
+0

Trong điều khiển của bạn, các @Path có thể phải có phần mở rộng .html. –

+0

Tôi có cùng một vấn đề nhưng thay vì lỗi 404 tôi nhận được 405. Vấn đề theo những gì tôi hiểu là xử lý tài nguyên chỉ chấp nhận yêu cầu GET vì vậy khi tôi chuyển hướng đến index.html sau khi đăng biểu mẫu có yêu cầu POST và chuyển hướng đến chỉ mục .html nếu người dùng được đăng ký! – Adelin

+0

Cảm ơn bạn đã đăng giải pháp. Các chủ đề khác thiếu sự kết hợp quyền của ánh xạ tài nguyên và InternalResourceViewResolver. –

Trả lời

7

Kiểm tra này ra cho các tập tin bản đồ html trong Spring MVC (Chi tiết bước được đưa ra trong trả lời):

Which spring view resolver plays nice with angularjs?

Trong đơn giản:

Để sử dụng tài nguyên tĩnh (html, css, img, js) vào mùa xuân, sử dụng một cấu trúc thư mục đó trông giống như sau:

src/ 
    package/ 
    LayoutController.java 
WebContent/ 
    WEB-INF/ 
    static/ 
     html/ 
     layout.html 
     images/ 
     image.jpg 
     css/ 
     test.css 
     js/ 
     main.js 
    web.xml 
    springmvc-servlet.xml 


@Controller 
public class LayoutController { 

@RequestMapping("/staticPage") 
public String getIndexPage() { 
return "layout.htm"; 

} } 




<!-- in spring config file --> 
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" /> 

layout.html

<h1>Page with image</h1> 
<img src="/static/img/image.jpg"/> 
+0

Cảm ơn bạn đã trả lời. Tôi đã cập nhật cấu trúc thư mục của mình cho cấu trúc thư mục bạn đã đề xuất. Tôi cũng đã thêm ánh xạ tài nguyên vào tệp cấu hình mùa xuân. Thật không may, tôi vẫn thấy cùng một vấn đề - điều khiển phương pháp được gọi, nhưng "không có bản đồ tìm thấy ..." trong giao diện điều khiển. Tôi đã cố gắng triển khai quan điểm giải quyết khác nhau và đã cố gắng thêm ".html" vào tên xem trả lại (bây giờ nó trông cho 'home.html.jsp'). – Mac

2

Điều này là do thường *.jsp kiểu mẫu uri được xử lý bởi container servlet và trong trường hợp cụ thể này *.html không phải là handl ed bởi container và thay vào đó đường dẫn được giao cho Spring MVC mà không biết làm thế nào để kết xuất các phần mở rộng này.

Ví dụ, nếu bạn đang sử dụng tomcat, bạn sẽ thấy những mục dưới conf/web.xml file:

<servlet-mapping> 
    <servlet-name>jsp</servlet-name> 
    <url-pattern>*.jsp</url-pattern> 
    <url-pattern>*.jspx</url-pattern> 
</servlet-mapping> 

tức là jsp servlet xử lý * .jsp, * mở rộng .jspx.

Vì vậy, cho này, cách khắc phục tiềm năng sẽ có thêm .html để được thêm vào được xử lý bởi jsp servlet, như trong liên kết này:

Using .html files as JSPs

hoặc thậm chí tốt hơn, để lại phần mở rộng là. jsp và sử dụng .html làm mẫu điều khiển thay thế?

+0

Cảm ơn đã gợi ý. Tôi đã thử thêm ".html" vào tên chế độ xem đã trả về, nhưng hiện tại, bạn đang cố gắng giải quyết tên chế độ xem "home.html.jsp". Nếu có một cách để tránh thay đổi các tập tin cấu hình tomcat, tôi sẽ thích cách tiếp cận đó. Cảm ơn một lần nữa. – Mac

-1
//Finally a working solution both html and jsp view together 
------------------------------------------------------------------------ 

public class ChainableInternalResourceViewResolver extends InternalResourceViewResolver { 

    private static Log logger = LogFactory.getLogger(ChainableInternalResourceViewResolver.class); 

    /** 
    * 
    */ 
    protected AbstractUrlBasedView buildView(String viewName) throws Exception { 
     logger.entering("buildView"); 
     String url = getPrefix() + viewName + getSuffix(); 
     InputStream stream = getServletContext().getResourceAsStream(url); 
     if (stream == null) { 
      logger.log(Log.DEBUG,"-----!!!------resource not found-------!!!-----"+getPrefix() + viewName + getSuffix()); 
      return new NonExistentView(); 
     } else { 
      logger.log(Log.DEBUG,"----***-------resource found-------***-----"+getPrefix() + viewName + getSuffix()); 
      stream.close(); 
     } 
     return super.buildView(viewName); 
    } 

    /** 
    * 
    * @author 
    * 
    */ 
    private static class NonExistentView extends AbstractUrlBasedView { 

     //private static Log logger = LogFactory.getLogger(NonExistentView.class); 

     protected boolean isUrlRequired() { 
      //logger.entering("isUrlRequired"); 
      return false; 
     } 

     public boolean checkResource(Locale locale) throws Exception { 
      //logger.entering("checkResource"); 
      return false; 
     } 

     protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, 
       HttpServletResponse response) throws Exception { 
      //logger.entering("renderMergedOutputModel"); 
      // Purposely empty, it should never get called 
     } 
    } 
} 

---------------------------------------------------------------------------- 
@EnableWebMvc 
@Configuration 
@ComponentScan({ "com.*" }) 
public class ApplicationConfig extends WebMvcConfigurerAdapter { 

    //Be careful while changing here 
    private static final String VIEW_DIR_HTML = "/WEB-INF/static/"; 
    private static final String VIEW_EXTN_HTML = ".html"; 

    private static final String VIEW_DIR_JSP = "/WEB-INF/"; 
    private static final String VIEW_EXTN_JSP = ".jsp"; 

    private static final String RESOURCE_URL_PATTERN_1 = "/resources/**"; 
    private static final String RESOURCE_URL_PATTERN_2 = "/WEB-INF/static/**"; 
    private static final String RESOURCE_PATH_1 = "/resources/"; 
    private static final String RESOURCE_PATH_2 = "/WEB-INF/static/"; 

    private static Logger logger = LoggerFactory.getLogger(ApplicationConfig.class); 

    /** 
    * 
    * @return 
    */ 
    @Bean 
    public ViewResolver htmlViewResolver() { 
     logger.info(" htmlViewResolver method "); 
     InternalResourceViewResolver viewResolver= new ChainableInternalResourceViewResolver(); 
     viewResolver.setPrefix(VIEW_DIR_HTML); 
     viewResolver.setSuffix(VIEW_EXTN_HTML); 
     viewResolver.setOrder(0); 
     return viewResolver; 
    } 

    @Bean 
    public ViewResolver jspViewResolver() { 
     logger.info(" jspViewResolver method "); 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setPrefix(VIEW_DIR_JSP); 
     viewResolver.setSuffix(VIEW_EXTN_JSP); 
     viewResolver.setOrder(1); 
     return viewResolver; 
    } 

-------------------------------------------------------------------------------- 
return "pages/login"; // for login.html resides inside /WEB-INF/static/pages/login.html 

return "jsp/login"; // for login.jsp resides inside /WEB-INF/jsp/login.jsp 
0
I was also facing the same issue and tried various solutions to load the AngularJS html file using Spring configuration. After applying below steps it got resolved. 

Step-1 in server's web.xml commemt these two lines 

<!--  <mime-mapping> 
     <extension>htm</extension> 
     <mime-type>text/html</mime-type> 
    </mime-mapping>--> 
<!--  <mime-mapping> 
     <extension>html</extension> 
     <mime-type>text/html</mime-type> 
    </mime-mapping> 
--> 

Step-2 enter following code in application's web xml 

    <servlet-mapping> 
    <servlet-name>jsp</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 


Step-3 

create a static controller class 

@Controller 
public class StatisController { 
    @RequestMapping("/landingPage") 
    public String getIndexPage() { 
    return "CompanyInfo"; 

    } 

} 
Step-4 in the Spring configuration file change the suffix to .htm 
     <property name="suffix"> 
      <value>.htm</value> 
     </property> 

Step-5 
Rename page as .htm file and store it in WEB-INF and build/start the server 

localhost:8080/.../landingPage 
+0

Hãy tự làm quen với [markdown định dạng] (http://stackoverflow.com/help/formatting) sử dụng ở đây tại SO. – grochmal

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