简体   繁体   English

没有视图解析器但纯HTML的Spring

[英]Spring with no view resolver but plain html

I'm trying to configure Spring to use only my html files instead of jsp view resolver but can't get it to work. 我试图将Spring配置为仅使用我的html文件而不是jsp视图解析器,但无法使其正常工作。 I tried many different configurations and I simply want to have redirection to /WEB-INF/views/index.html everytime localhost:8080/ is entered. 我尝试了许多不同的配置,并且我只想每次输入localhost:8080 /时都重定向到/WEB-INF/views/index.html。 Now what I have in my tomcat console is: 现在,我的tomcat控制台中的内容是:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/views/index.html] in DispatcherServlet with name 'appServlet'. org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名称为“ appServlet”的URI [/WEB-INF/views/index.html]的HTTP请求未找到映射。

here's my servlet-context.xml snippet. 这是我的servlet-context.xml代码段。

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

Any suggestions what am I missing? 有什么建议我想念什么?

EDIT- web.xml: 编辑-web.xml:

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

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 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> 

I know this is an older question so this is for anyone who comes here now. 我知道这是一个比较老的问题,所以这是给现在来这里的人的。 Spring is auto-configured to pick up your views as long as your file paths are correct, so you should not have to do any spring webmvc configuration . 只要文件路径正确,Spring就会自动配置为获取您的视图,因此您不必进行任何spring webmvc configuration

I ran into this issue in Intellij and simply had to restart the application and the error was gone. 我在Intellij中遇到了这个问题,只需要重新启动应用程序,错误就消失了。 Hope someone sees this and doesn't spend hours working on this. 希望有人看到这个并且不会花很多时间在此上。

This error 这个错误

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

is telling us that you are actually going to the following address 告诉我们您实际上要转到以下地址

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

which obviously makes no sense. 这显然没有意义。

If you want 如果你想

to have redirection to /WEB-INF/views/index.html everytime localhost:8080/ is entered 每次输入localhost:8080 /时都重定向到/WEB-INF/views/index.html

add a welcome file to your deployment descriptor. 将欢迎文件添加到您的部署描述符。

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

Try to add a / to the prefix. 尝试在前缀前添加/。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM