【问题标题】:HTTP Error 404. The requested resource is not found. Spring MVCHTTP 错误 404。未找到请求的资源。春季MVC
【发布时间】:2016-03-21 11:26:50
【问题描述】:

我是 Spring MVC 的新手。在尝试基本程序时,我遇到了 404 错误页面。

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>My Demo App</display-name>
    <servlet>
        <servlet-name>myDemoApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>myDemoApp</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

myDemoApp-servletConfid.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <mvc:annotation-driven/>

    <!-- Components Scanner...package name should be the one which is having the controllers -->
    <context:component-scan base-package="com.demo.controllers"></context:component-scan>

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

控制器类

package com.demo.controllers;

import java.util.Random;

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

@Controller
public class MyDemoController {

    private String[] quotes = {"Hi I am vikram,"
            + "I am from Bangalore,"
            + "Bangalore is full of traffic"};

    @RequestMapping("/getQuote")
    public String getRandomQuote(Model model){

        int rand = new Random().nextInt(quotes.length);
        String randomQuote = quotes[rand];

        //http://localhost:8080/springMVCDemo/getQuote.html
        model.addAttribute("randomQuote",randomQuote);
        return "quote";
    }

}

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>My Demo App</title>
</head>
<body>
    <h1>The Quote is: </h1>
    <p>${randomQuote}</p>
</body>
</html>

使用的网址:

http://localhost/8050/springMVCDemo/getQuote.html

文件夹结构: [在此处输入图片说明][1]

请向我提出错误的解决方案。

【问题讨论】:

  • 开启调试日志,你可能会得到更好的线索。
  • 您使用什么服务器来运行这个应用程序?服务器配置中是否正确设置了上下文根“springMVCDemo”?
  • 这个post 可能会帮助您正确设置上下文配置位置。在web.xml 中声明ContextLoaderListener 并正确初始化context-param
  • 与问题无关,但您在 bean 配置文件中放置了两次 InternalResourceViewResolver bean
  • @Pragnani 第一个被评论。

标签: java spring spring-mvc


【解决方案1】:

从错误中可以清楚地看出 spring 未能根据您当前的配置加载控制器。始终关注调试日志,并查看启动时使用 spring 容器注册的 bean。日志记录将帮助您更好地解决这个问题。

尝试使用 Bootstrap 侦听器 ContextLoaderListener,它将从 contextConfigLocation 中设置的 context-param 读取和加载配置

例如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/myDemoApp-servletConfid.xml</param-value>
</context-param>

<listener>
   <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener> 

关注相关post

【讨论】:

    【解决方案2】:

    更改 myDemoApp(调度程序)servlet 映射如下。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2012-12-27
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多