无法在我的spring应用程序中加载html文件

时间:2015-12-07 17:37:13

标签: java html spring spring-mvc

我正在尝试加载我的Spring应用程序,我只有html文件要呈现,但它无法显示任何html文件。它始终显示404错误。但是当我尝试使用tomcat直接运行html文件时,它将能够显示我的html文件。

以下是我的目录结构

enter image description here

enter image description here

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 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>

    <!-- 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>ankServlet</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>ankServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <context:annotation-config />

    <!-- Resolves views selected for rendering by @Controllers to .htmlresources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/resources/html/" />
        <beans:property name="suffix" value=".html" />
    </beans:bean>

    <context:component-scan base-package="com.ankit" />

</beans:beans>

HomeController.java

@Controller
public class HomeController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String displayLogin(HttpServletRequest request, HttpServletResponse response)
    {   
                return "login"; 
    }

1 个答案:

答案 0 :(得分:0)

尝试改变这一点:

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

要:

   <servlet-mapping>
    <servlet-name>ankServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>