2015-02-23 129 views
-2

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

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 
+1

是否试图在浏览器中打开页面?怎么了? – 2015-02-23 12:04:19

+0

你试过用'/*'? – 2015-02-23 12:04:53

+0

你好像缺少mvc:annotation-driven元素,http://stackoverflow.com/questions/28364163/getting-warn-org-springframework-web-servlet-pagenotfound-no-mapping-found-fo/28364345#28364345 – 2015-02-23 12:07:57

回答

0

我猜MVC:注解驱动因素应该在你的moqahServices使用,检查出来

0

寻找到你的代码看起来,

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

2)在web.xml考虑servlet-namemoqahServices,弹簧配置名称应与-servlet.xml随后servlet-name即后缀。预计您的弹簧配置名称为moqahServices-servlet.xml

请尝试执行上述更改。