Spring MVC:Controller Mapping有效但返回给出404

时间:2014-11-02 16:06:40

标签: java spring spring-mvc mapping

我正在尝试创建一个Spring MVC Web应用程序。这是我的配置:

的web.xml     

<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>bookstore-presentation</display-name>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:config/bookstore-presentation-context.xml,
                classpath*:config/bookstore-presentation-security.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:config/bookstore-domaine-context.xml,
            classpath*:config/bookstore-domaine-datasource.xml,
            classpath*:config/bookstore-service-context.xml,
            classpath*:config/bookstore-presentation-security.xml,
            WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>


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

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:property-placeholder location="classpath*:bookstore.properties" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>WEB-INF/Views/</value>
        </property>
        <property name="suffix">
            <value>.html</value>
        </property>
    </bean>


    <mvc:resources mapping="/static/**" location="Assets"/>

    <mvc:default-servlet-handler/>


</beans>

HomeController.java

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        int a = 0;

        a += 1;
        return "index";
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
}

当我调试我的代码并尝试&#34; /&#34;或&#34; / login&#34;映射,我看到调用了正确的方法,但是当我返回相应的字符串时,我得到404错误。

我的文件夹像这样组织

webapp
--META-INF/
----MANIFEST.MF
--WEB-INF/
----Assets/
------css/
------img/
------js/
----Views/
------index.html
------login.html
----dispatcher-servlet.xml
----web.xml

我哪里出错了?

感谢您的帮助!

0 个答案:

没有答案