【问题标题】:404 error for Spring MVC Rest URISpring MVC Rest URI 的 404 错误
【发布时间】:2015-04-24 18:06:03
【问题描述】:

我正在为 Spring MVC 制作一个虚拟的 hello world 应用程序,我所做的与示例中提到的完全相同,但是当我运行 URL 时,它给了我 404 错误。如果可以,请帮助我,以下是我的文件:-

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>Spring MVC Application</display-name>
   <mvc:default-servlet-handler default-servlet-name="moqahServices"/>
   <servlet>
      <servlet-name>moqahServices</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

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

   <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error.jsp</location>
  </error-page>

</web-app>


    moqahServices.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="com.moqah" />

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

</beans>

 HelloController.jsp

    package com.moqah;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }
}



Please let me know if you have any suggestions

【问题讨论】:

  • 尝试在浏览器中打开页面?会发生什么?
  • 你试过&lt;url-pattern&gt;/*&lt;/url-pattern&gt;吗?
  • 您似乎缺少 mvc:annotation-driven 元素,可能与 stackoverflow.com/questions/28364163/… 重复

标签: java spring spring-mvc


【解决方案1】:

我猜你的 moqahServices 中应该使用 mvc:annotation-driven 元素,检查一下

【讨论】:

    【解决方案2】:

    看起来你的代码,

    1) 你在错误的地方配置了mvc:default-servlet-handler。它应该是您的弹簧配置的一部分。

    2) 在web.xml 中,将servlet-name 视为moqahServices,弹簧配置名称应以-servlet.xml 为后缀,后跟servlet-name 即。您的 spring 配置名称应为 moqahServices-servlet.xml

    请尝试进行上述更改。

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2011-10-04
      • 2013-08-14
      • 2013-12-13
      • 2017-03-15
      • 2014-04-05
      相关资源
      最近更新 更多