Spring MVC ViewResolver没有映射到HTML文件 [英] Spring MVC ViewResolver not mapping to HTML files

查看:469
本文介绍了Spring MVC ViewResolver没有映射到HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让Spring MVC解析.html视图文件.

I cannot get spring mvc to resolve .html view files.

我具有以下视图文件夹结构:

I have the following view folder structure:

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

我有一个简单的hello world控制器方法,只打印一条消息 并返回视图名称"home".我有一个home.jsp文件,但想 改用home.html.

I have a simple hello world controller method that just prints a message and returns the view name "home". I have a home.jsp file, but would like to use the home.html instead.

<!-- 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>

当我击中宇宙飞船/房屋时,控制器会打印"hello world"消息,并且我看到 home.jsp视图没有问题.

When I hit spaceships/home the controller prints the hello world message and I see the home.jsp view without a problem.

问题是当我将后缀更改为.html时.

The problem is when I change the suffix to .html.

更改后缀并导航到/home后,控制器将打印 消息,但是我在浏览器中看到404错误,在控制台中看到以下错误: 警告:找不到带有URI [/spaceships/WEB-INF/views/home.html]的HTTP请求的映射

After changing the suffix and navigating to /home, the controller prints the message however I see a 404 error in the browser and the following in the console: WARNING: No mapping found for HTTP request with URI [/spaceships/WEB-INF/views/home.html]

要澄清:

<!-- 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>

我已经检查了展开的war文件夹,可以确认两个主文件都存在.

I have checked in the exploded war folder and can confirm that both home files are present.

以前有没有人遇到过类似的事情?

Has anyone encountered something like this before?

最后一则控制台消息:

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'

感谢阅读.

===========解决方案===========

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

以下(较丑陋的)配置解决了该问题.可能有多种方法可以解决此问题,但是如果您遇到相同的问题,则可以从中解决一个问题.

The following (ugly) configuration solved the issue. There are probably ways to clean this up, but if you are experiencing the same problem you may be able to piece together a solution from this.

文件夹结构:

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

控制器方法:

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

Spring配置:

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

推荐答案

在Spring mvc中检查html文件的映射(详细步骤在答案"中给出):

Check this out for mapping html files in Spring mvc (Details step is given in Answer):

哪个spring view解析器可以与angularjs配合使用?

简单地说:

为了在春季使用静态资源(html,css,img,js),请使用如下所示的目录结构:

In order to use static resource(html,css,img,js) in spring, use a directory structure that looks like the following:

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

layout.html

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

这篇关于Spring MVC ViewResolver没有映射到HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆