【问题标题】:Spring with no view resolver but plain html没有视图解析器但纯 html 的 Spring
【发布时间】:2014-01-10 08:05:51
【问题描述】:

我正在尝试将 Spring 配置为仅使用我的 html 文件而不是 jsp 视图解析器,但无法使其工作。我尝试了许多不同的配置,我只想在每次输入 localhost:8080/ 时重定向到 /WEB-INF/views/index.html。现在我在我的 tomcat 控制台中拥有的是:

org.springframework.web.servlet.PageNotFound - 找不到映射 DispatcherServlet 中带有 URI [/WEB-INF/views/index.html] 的 HTTP 请求 名称为“appServlet”。

这是我的servlet-context.xmlsn-p。

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="WEB-INF/views/" />
    <beans:property name="suffix" value="" />
</beans:bean>
<view-controller path="/" view-name="index.html"/>

任何建议我缺少什么?

编辑- web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>

    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>/WEB-INF/views/index.html</welcome-file>
</welcome-file-list> 

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我知道这是一个较老的问题,所以这是为现在来到这里的任何人准备的。只要您的文件路径正确,Spring 就会自动配置为获取您的视图,因此您不必执行任何spring webmvc configuration

    我在 Intellij 中遇到了这个问题,只需重新启动应用程序,错误就消失了。希望有人看到这一点,不要花几个小时在这上面工作。

    【讨论】:

      【解决方案2】:

      这个错误

      org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/views/index.html] in DispatcherServlet with name 'appServlet'.
      

      告诉我们您实际上要去以下地址

      localhost:8080/WEB-INF/views/index.html
      

      这显然没有意义。

      如果你愿意

      每次都重定向到 /WEB-INF/views/index.html localhost:8080/ 已输入

      将欢迎文件添加到您的部署描述符中。

      <welcome-file-list>
          <welcome-file>WEB-INF/views/index.html</welcome-file>
      </welcome-file-list>
      

      【讨论】:

      • 现在我有WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'appServlet' :(
      • @xwhyz 您的DispatcherServlet 映射到什么网址?当您使用它时,只需发布​​您的完整配置。
      • @xwhyz 文件WEB-INF/views/index.html 真的存在于您所说的位置吗?
      • s21.postimg.org/6fvdizbsn/paaaath.png 就是现在的样子,我有两个 index.html '以防万一'
      • @xwhyz 只是为了尝试找出问题所在,删除您的 &lt;servlet&gt; 声明,看看会发生什么。重新编译并重建您的项目。检查你的 servlet-context 配置是否有可能干扰的东西不会有什么坏处。
      【解决方案3】:

      尝试在前缀中添加 /。

      <beans:bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <beans:property name="prefix" value="/WEB-INF/views/" />
          <beans:property name="suffix" value="" />
      </beans:bean>
      <view-controller path="/" view-name="index.html"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 2014-01-29
        • 2011-07-20
        • 2018-03-10
        相关资源
        最近更新 更多