RequestMapping不起作用,404

时间:2016-04-20 19:24:41

标签: java spring spring-mvc

我知道,有很多这样的问题,但我已经阅读了所有的答案,但他们没有帮助我。

我一直试图从控制器获取jsp时出现404错误。

我的web.xml:

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

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.json</url-pattern>
        <url-pattern>*.xml</url-pattern>
    </servlet-mapping>

</web-app>

(我试图只使用一个<url-pattern>/</url-pattern>,但没有帮助。)

我的dispatcher-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"
       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">

    <context:component-scan base-package="olegshan.agg.jba.controller"/>

</beans>

我的IndexController.java:

package olegshan.agg.jba.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index() {
        return "/WEB-INF/jsp/index.jsp";
    }
}

当我输入我的地址行时,例如,http://localhost:8080/index.html它不起作用,我变成了404。

即使在地址行http://localhost:8080/WEB-INF/jsp/index.jsp中直接输入,我仍然无法丰富我的index.jsp - 我变成了404错误。

我可以丰富我的网络文件夹中的文件,例如http://localhost:8080/hello.jsp,它可以正常工作。但是,当我要求我的Controller方法返回此文件时,如下所示:return "/hello.jsp";它仍然不起作用。

我使用Spring 4.2.5,Tomcat 8.0.33,Intellij Idea 2016.1.1。

请帮助我。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

试试这个。

我的web.xml:

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

在浏览器中输入类似@RequestMapping的内容,例如http://localhost/your_context/hello

此外,一旦请求进入控制器,视图应由一个viewResolver处理。

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

然后,在你的控制器中,只需要这个。

@RequestMapping(value = "/hello")
public String index(){

    return "index";
}

答案 2 :(得分:0)

我发现了我的问题,它在pom.xml中发出 - 我忘记了:<packaging>war</packaging>

所以我的代码是正确的。

感谢大家帮助我。

相关问题