2015-12-04 52 views
0

我需要在春季3中單獨運行此項目。我不能使用彈簧引導或任何其他技術堆棧。春季調度員Servlet映射錯誤3

我試圖寫一個簡單的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"> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

彈簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
    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.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan 
     base-package="com.hpe" /> 
    <mvc:annotation-driven /> 

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

HelloWorldController.java

package com.hpe; 

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

@Controller 
public class HelloWorldController { 

    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public ModelAndView sayHello() { 
     String message = "Hello World, Spring 3.0!"; 
     return new ModelAndView("hello", "message", message); 
    } 
} 

的hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>Insert title here</title> 
    </head> 
    <body> 
     <h1>Hello World</h1>  
    </body> 
</html> 

每當我嘗試訪問localhost:8080/{context-name}/hello時,我得到一個404錯誤。

+0

在您正在部署該服務器有哪些?你的上下文路徑是什麼? –

+0

我正在部署在tomcat 7服務器上。我的上下文路徑是「HPAssignment」 – Rammohan

+0

tomcat在eclipse中配置?或獨立? –

回答

0

你試試這個簡單的,並告訴我這是工作YAA不

@RequestMapping(value = "/hello", method = RequestMethod.GET) 
    public String sayHello() { 
     return "hello"; 
    } 
+0

不,它不起作用 – Rammohan

+0

爲什麼它會工作? – SpringLearner

+0

@SpringLearner - 請告訴我代碼中的錯誤在哪裏。 – Rammohan

0

添加@RequestMapping("/")類級別的註釋上HelloWorldController

+0

仍然無法正常工作。 – Rammohan

+0

您的contextPath是否正確?這是你在'eclipse - > tomcat - > modules - > Path'中設置的內容。還有什麼是你得到的完整錯誤? –

+0

並確保你的'hello.jsp'在'WEB-INF/jsp'文件夾中 –

0

該代碼看起來是正確的。嘗試

1)驗證您的上下文根 - >http://localhost:8090/%nameofwar %/你好

2)檢查,如果戰爭是越來越正確建立。檢查是否所有像web.xml中所需的構件,JSP的.class,.XML在正確的地方

3)部署在獨立的Tomcat和檢查