【问题标题】:Dispatcher Servlet mapping error in spring 3Spring 3中的Dispatcher Servlet映射错误
【发布时间】:2016-03-09 00:07:37
【问题描述】:

我需要在 Spring 3 单独运行这个项目。我不能使用 Spring Boot 或任何其他技术堆栈。

我尝试编写一个简单的 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>

spring-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);
    }
}

你好.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 错误。

【问题讨论】:

  • 您在哪台服务器上部署?你的上下文路径是什么?
  • 我正在部署在 tomcat 7 服务器上。我的上下文路径是“HPAssignment”
  • 在eclipse中配置tomcat?还是独立的?
  • 在eclipse中配置
  • 你的基础包是对的

标签: spring spring-mvc spring-3


【解决方案1】:

你试试这个简单的,告诉我它是有效的,但不是

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

【讨论】:

  • @SpringLearner - 你能告诉我代码中的错误在哪里吗?
  • @Rammohan 你会平铺吗?
  • 请给我所有项目层次结构的详细信息,您可以在其中放置 pojo 类、jsp 文件、控制器等...
【解决方案2】:

HelloWorldController 上添加@RequestMapping("/") 类级别注释。

【讨论】:

  • 您的 contextPath 是否正确?这是您在 eclipse -&gt; tomcat -&gt; modules -&gt; Path 中设置的内容。另外你得到的完整错误是什么?
  • 并确保您的hello.jspWEB-INF/jsp 文件夹中
  • 我在 WEB-INF/jsp 文件夹中有 hello.jsp
  • 好的。试试这个网址 - localhost:8080/HPAssignment/{yourWarFileName}/hello
  • 这很奇怪!您是否尝试将应用程序部署到独立的 tomcat?
【解决方案3】:

代码在我看来是正确的。试试

1) 验证你的上下文根 ->http://localhost:8090/%nameofwar %/你好

2) 检查战争是否正确构建。检查所有必需的工件,如 web.xml、jsp、.class、.xml 是否都在正确的位置

3) 部署在独立的 Tomcat 中并检查

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 2011-12-27
    • 2017-01-18
    • 2016-06-30
    • 2017-03-22
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多