2014-08-31 136 views
0

我打字的URL後收到此錯誤 本地主機:8080 /的HelloWebSpring MVC的HTTP狀態404錯誤

HTTP Status 404 - /HelloWeb 

type Status report 

message /HelloWeb 

description The requested resource is not available. 
Apache Tomcat/7.0.30 

我不能夠解決這個問題,請幫助別人 這裏是我所需要做的文件執行此Spring MVC的招呼程序

HelloController.java

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

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

     return "hello"; 
    } 
} 

的hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %> 
<html> 
<head> 
<title>Hello World</title> 
</head> 
<body> 
    <h2>${message}</h2> 
</body> 
</html> 

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

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

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

</web-app> 

的HelloWeb-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="com.tutorialspoint" /> 

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

</beans> 

,這裏是我的項目層次

的HelloWeb
  SRC
    com.tutorialspoint
 的WebContent
      JSP
         的hello.jsp
      META-INF
          MANIFEST.MF
      WEB_INF
          LIB
         的HelloWeb-servlet.xml中
         的index.jsp
         的web.xml

對不起,我忘了包括以下我從

tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

+0

我也得到同樣的錯誤。你有解決方案嗎? – Chetan 2015-02-18 06:24:13

+0

@Chetu不,我沒有。 – 2015-02-18 06:58:09

回答

0

您使用了錯誤的URL這個Spring MVC的例子。根據您的部署方式,正確的URL是localhost:8080/hello或localhost:8080/HelloWeb/hello。

+0

沒有先生@atamanroman我使用URL本地主機:8080/HelloWeb /但只要我輸入Http狀態404錯誤來了。 – 2014-08-31 08:35:25

0

當你點擊url localhost:8080/HelloWeb/它需要一個登陸頁面來顯示你可以在web.xml中指定的東西。添加到您的web.xml下

<welcome-file-list> 
    <welcome-file>/WEB-INF/index.jsp</welcome-file> 
</welcome-file-list> 

,但如果你需要訪問servlet需要指定URL作爲 localhost:8080/HelloWeb/hello

+0

我想要顯示servlet,但只要我輸入localhost:8080/HelloWeb/hello但是再次出現錯誤Http Status 404出現 – 2014-08-31 09:01:08

+0

你是什麼意思通過顯示servlet和你的url必須是localhost:8080/HelloWeb/hello HelloWeb用大寫H – SparkOn 2014-08-31 09:05:34

+0

此外,您在HelloWeb-servlet.xml中缺少'' – SparkOn 2014-08-31 09:09:33

0

這是晚了,但我也有同樣的問題,因爲我也採取了代碼tutorialspoint.com

嘗試添加這在HelloWeb-servlet.xml

<context:annotation-config /> 

這將使annotat離子

和以及包括這在你的web.xml

<welcome-file-list> 
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> 
</welcome-file-list> 
0

我有同樣的問題,發現我的JSP名稱有一個空格。來自tutorialspoint的示例完美無缺。

+1

這是一個比真實的答案更多的評論,因爲你只是猜測。 – maRtin 2015-10-10 14:39:12

0

我知道這篇文章很舊,這是爲了其他可能面臨同樣挑戰的人。 您的網址映射未正確映射。使用這個作爲你的控制器。一個變化就是「你好」到「的HelloWeb」你Controoler名稱(@RequestMapping(「/你好」))應該是相同的web應用程序( 的HelloWeb / )

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

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

      return "hello"; 
     } 
    } 
名稱
0

這是你的代碼...

`<property name="prefix" value="/WEB-INF/jsp/" />` 

it shows that you have folder 'jsp' inside the 'WEB-INF' . 

但你沒有一個文件夾中有通過看你的項目層次結構。

因此,更改代碼到下面:

`<property name="prefix" value="/WEB-INF/" />.`