无法在名为Y的servlet中解析名称为X的视图

时间:2016-05-27 12:02:30

标签: spring spring-mvc view

我使用Spring和AngularJS作为休息Web应用程序。基于this教程并基于this教程,我创建了以下文件夹结构:

-src
|----main
    |----webapp
        |----static
            |----api
            |----assets
            |----resources
            |----sections
            |----services
            |----index.html
        |----WEB-INF
            |----web.xml
            |----spring
                |----dispatcher-servlet.xml
                |----applicationContext.xml

我想提供静态HTML文件,所以我将资源声明如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="org.tools.mvc.controllers"></context:component-scan>
<mvc:resources location="/static/" mapping="/**"/>
<mvc:resources location="/static/api/" mapping="/api/**"/>
<mvc:resources location="/static/assets/" mapping="/assets/**"/>
<mvc:resources location="/static/resources/" mapping="/resources/**"/>
<mvc:resources location="/static/services/" mapping="/services/**"/>
<mvc:resources location="/static/sections/home/" mapping="/sections/home/**"/>


<bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>
<bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>

我的问题是,当我部署应用程序(我正在使用Glassfish服务器)时,会发生以下错误(在Eclipse控制台和浏览器页面中):

2016-05-27T04:46:06.640-0700|Warning: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception
javax.servlet.ServletException: Could not resolve view with name 'index.html' in servlet with name 'dispatcher'

服务的功能不受影响(使用Postman成功测试其余的api结果)。到目前为止,为了进一步开发应用程序,我手动将index.html添加到URL的末尾(如localhost:8080 / status / index.html)。 Web部署程序集中声明的“/”源是:

来源:/ src / main / webapp

部署路径:/

更新:web.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>status</display-name>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

如何解决此错误?

谢谢

P.S在SO上找到的解决方案并不适用于我的场景。

1 个答案:

答案 0 :(得分:0)

我发现我在applicationContext.xml中声明了一个视图解析器(BeanNameViewResolver)。删除视图解析程序后,我没有收到错误。

发生错误是因为Spring试图将我的视图(女巫是一个html静态文件)解释为bean。