简体   繁体   English

Spring MVC应用程序出现404错误

[英]Spring MVC application with 404 error

My Spring release is 5.x and tomcat version is 8.5, so according to introduction, they will support the web application running without web.xml, but I got 404 error, see my project structure below: 我的Spring版本是5.x,tomcat版本是8.5,因此根据介绍,它们将支持不带web.xml的Web应用程序运行,但是出现404错误,请参见下面的项目结构:

在此处输入图片说明

I use this url to access my application, but got 404 error: 我使用此URL访问我的应用程序,但收到404错误:
http://localhost:8080/SpringMVC/ HTTP://本地主机:8080 /用SpringMVC /

see my code below: 请参阅下面的代码:

RootConfig.java: RootConfig.java:

package spittr.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages= {"spitter"},
               excludeFilters= {@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)}
              )
public class RootConfig {

}

WebConfig.java: WebConfig.java:

package spittr.config;

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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig implements WebMvcConfigurer{

    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
    {
        configurer.enable();
    }
}

SpittrWebAppInitializer.java: SpittrWebAppInitializer.java:

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] {RootConfig.class}; 
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

HomeController.java: HomeController.java:

package spittr.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String home()
    {
        System.out.println("test");
        return "home";
    }
}

home.jsp: 针对home.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>this is the Home Jsp page</title>
</head>
<body>
    <h1>Hello, Spring MVC!</h1>
</body>
</html>

from the Eclipse console, I can see output "test" that means spring context find out the controller, but seems it cannot find the jsp page, I don't know the reason, can someone tell me what's wrong with my code? 从Eclipse控制台中,我可以看到输出“ test”,这意味着spring上下文可以找到控制器,但是似乎找不到jsp页面,我不知道原因,有人可以告诉我我的代码有什么问题吗?

Try adding @Bean annotation on top of WebConfig#viewResolver() method. 尝试在WebConfig#viewResolver()方法的顶部添加@Bean批注。 So, the Spring Container manage your method as bean and your custom configuration will probably work. 因此,Spring Container将您的方法作为Bean管理,您的自定义配置可能会起作用。

@Bean
public ViewResolver viewResolver(){}

Indicates that a method produces a bean to be managed by the Spring container. 指示方法产生一个由Spring容器管理的bean。

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

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