简体   繁体   English

春季MVC HTTP状态404

[英]spring MVC HTTP Status 404

I followed this tutorial to make a simple MVC project, and I added one more controller and view. 我按照本教程制作了一个简单的MVC项目,并添加了另一个控制器和视图。 But now, whichever link out of following I use, I always get this error in chrome: 但是现在,无论我使用以下哪个链接,我总会在chrome中收到此错误:

HTTP Status 404 - /spring-MVC

type Status report

message /spring-MVC

description The requested resource is not available.

Apache Tomcat/8.0.30

web.xml: web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>spring-MVC</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>spring_maven.spring_MVC</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

MvcConfiguration.java: MvcConfiguration.java:

package spring_maven.spring_MVC.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages="spring_maven.spring_MVC")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }


}

HomeController.java: HomeController.java:

package spring_maven.spring_MVC.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

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

@Controller
public class HomeController {


    @RequestMapping(value="/")
    public ModelAndView ejub(HttpServletResponse response) throws IOException{
        return new ModelAndView("home.jsp");
    }
}

PlacesController.java: PlacesController.java:

package spring_maven.spring_MVC.controller;


import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

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

@Controller
public class PlacesController {

    @RequestMapping(value="/getAllPlaces")
    public ModelAndView getAllPlaces() throws IOException{
        return new ModelAndView("getAllPlaces.jsp");
    }
}

home.jsp: home.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Home</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>This is the homepage!</p>
    </body>
</html>

getAllPlaces.jsp: getAllPlaces.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>getAllPlaces</title>
</head>
<body>
eeeee
</body>
</html>

and the structure of files in eclipse: 以及eclipse中文件的结构:

在此处输入图片说明

So as far as I see, these links should work, but they produce the mentioned errors: 就我所知,这些链接应该可以工作,但是它们会产生上述错误:

http://localhost:8080/spring-MVC http://localhost:8080/spring-MVC/getAllPlaces http://localhost:8080/spring-MVC/home http:// localhost:8080 / spring-MVC http:// localhost:8080 / spring-MVC / getAllPlaces http:// localhost:8080 / spring-MVC / home

What did I do wrong? 我做错了什么?

Your controller returns home.jsp for / so to get home url should look like http://localhost:8080/spring-MVC/ 您的控制器为/返回home.jsp,因此获取主目录网址应类似于http:// localhost:8080 / spring-MVC /

ends with / , not /home 以/结尾,而不是/ home

And I guess you don't need to use .jsp in controller return statement: 而且我猜您不需要在控制器return语句中使用.jsp:

return new ModelAndView("home");

View resolver will do the rest 查看解析器将完成其余工作

Error occured because of two things: 1. I forgot to drag the project into the Tomcat server which was running. 由于两件事而发生错误:1.我忘记将项目拖到正在运行的Tomcat服务器中。 2. I forgot to restart the server. 2.我忘了重启服务器。

When I did these two thing, suddenly it started working.. :) 当我做这两件事时,突然开始工作了.. :)

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

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