简体   繁体   English

HTTP错误404。找不到请求的资源。 春季MVC

[英]HTTP Error 404. The requested resource is not found. Spring MVC

I'm new to Spring MVC. 我是Spring MVC的新手。 While trying for basic program I'm facing the 404 error page. 在尝试基本程序时,我面临404错误页面。

Web.xml 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 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>

Controller class 控制器类

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 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>

URL Used: 使用的网址:

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

Folder structure: [enter image description here][1] 文件夹结构: [在此处输入图像说明] [1]

Kindly suggest me the solution for the error. 请为我建议该错误的解决方案。

It's clear from the error that spring was failed to load the controller as per your current configuration. 从错误中可以明显看出,弹簧无法按照您当前的配置加载控制器。 Always keep an eye on debug logs and look what beans are registered with spring container on start-up. 始终注意调试日志,并在启动时查看在spring容器中注册了哪些bean。 Logging will help you better to solve this problem. 日志记录将帮助您更好地解决此问题。

Try with Bootstrap listener ContextLoaderListener that will read and load the configuration from contextConfigLocation set in context-param 尝试使用Bootstrap侦听器ContextLoaderListener ,它将从context-param设置的contextConfigLocation读取并加载配置

For example: 例如:

<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> 

Follow the related post 关注相关帖子

changes the myDemoApp (dispatcher) servlet mapping as below. 如下更改myDemoApp (调度程序)servlet映射。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM