2013-10-22 37 views
0

我是新來的Spring MVC框架,遵循這一site爲世界您好教程Spring MVC框架的Hello World顯示錯誤404

錯誤:所請求的資源不可用

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 
    <display-name>HelloWorldExampleWithSpring3MVCInEclipse</display-name> 
    <servlet> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/app-config.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

APP-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
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"> 
    <!-- Scans the classpath of this application for @Components to deploy as beans --> 
    <context:component-scan base-package="com.raistudies" /> 
    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven /> 
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

HelloWorldAction.java

package com.raistudies.actions; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class HelloWorldAction { 

    @RequestMapping(value="/hello",method=RequestMethod.GET) 
    public ModelAndView sayHello(Model model){ 
     ModelAndView mv = new ModelAndView(); 
     mv.setViewName("hello"); 
     model.addAttribute("HelloMessage", "Hello World from My First Spring 3 mvc application"); 
     return mv; 
    } 
} 

的hello.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<%@ page session="true" %> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Hello World with Spring 3 MVC</title> 
    </head> 
    <body> 
     <h1>Welcome! Spring MVC is working well.</h1><br /> 
     ${HelloMessage} 
    </body> 
</html> 
+0

你的代碼和配置似乎是正確的。您試圖從瀏覽器訪問哪個網址?你正在訪問'http:// localhost:8080/HelloWorldExampleWithSpring3MVCInEclipse/hello.htm'嗎? –

+0

是的,我使用相同的URL,甚至我創建了index.jsp,它拋出了相同的錯誤 –

+0

我沒有HelloWorldAction控制器在web inf中,但在src文件夾中。它有什麼區別 –

回答

0

如果您正在訪問http://localhost:8080/HelloWorldExampleWithSpring3MVCInEclipse/,你需要創建的WebContent/index.jsp的你項目來使這個URL工作。

的index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<jsp:forward page="/hello.htm"></jsp:forward> 

參見示例代碼here

1

web.xml當我們用「/」替換「* htm」時,則解決了資源不可用的錯誤。

1

原來的問題是,因爲你發送結束*.htm春的所有請求:

<servlet-mapping> 
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 

但是你的控制器被映射到/hello沒有擴展名。

變化控制器的@RequestMapping到:

@RequestMapping(value="/hello.htm",method=RequestMethod.GET)