【问题标题】:HTTP Status 404 in RequestMapping in SpringSpring RequestMapping中的HTTP状态404
【发布时间】:2014-04-24 02:15:32
【问题描述】:

我正在使用请求映射注解编写一个 Spring 应用程序。 http://localhost:8080/SpringMVC/hello 给出 404(请求的资源不可用)。从服务器记录数据`在 DispatcherServlet 中找不到带有 URI [/SpringMVC/WEB-INF/jsp/helloView.jsp] 的 HTTP 请求的映射,名称为“dispatcher”。 有人可以帮忙吗。

项目结构

@Controller 
    @RequestMapping(value="/hello")
    public class HelloController 
    {
    @RequestMapping(method=RequestMethod.GET) 
    public ModelAndView sayHello() 
    {
    ModelAndView view=new ModelAndView("helloView","HelloMessage", "Welcome"); 
    return view; 
    }

    }

helloView.jsp

<html>
<head>
<title>Spring  Application</title>
</head>
<body>
   <h2>${HelloMessage}</h2>
</body>
</html>

web.xml

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

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>  
</servlet-mapping>

</web-app>

调度程序-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->

    <context:component-scan base-package="controller" />
    <mvc:annotation-driven />

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

</beans>

【问题讨论】:

  • 控制台中有任何日志消息?

标签: spring spring-mvc


【解决方案1】:

您的 web.xml 有带有 url 模式 /* 的调度程序 servlet,它实际上映射了所有内容!所以,创建 / 并尝试...

【讨论】:

    【解决方案2】:

    您忘记在 dispatcher-servlet.xml 中添加&lt;context:annotation-config/&gt;,这会激活各种注释,例如:@Controller@Component@Service@Repository ..

    并通过映射到/将spring dispatcher设为Default Servlet,这样每个请求都会在dispatcher servlet中结束。

    <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet>
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>  
    </servlet-mapping>
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 2021-10-24
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多