2015-12-04 53 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和检查