【问题标题】:Spring MVC application with 404 errorSpring MVC 应用程序出现 404 错误
【发布时间】:2017-10-28 10:27:13
【问题描述】:

我的 Spring 版本是 5.x,tomcat 版本是 8.5,所以根据介绍,他们将支持在没有 web.xml 的情况下运行的 web 应用程序,但是我得到了 404 错误,请参见下面的项目结构:

我使用此网址访问我的应用程序,但收到 404 错误:
http://localhost:8080/SpringMVC/

请参阅下面的代码:

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:

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:

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:

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:

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

从 Eclipse 控制台,我可以看到输出“test”,这意味着 spring 上下文找到了控制器,但似乎找不到 jsp 页面,我不知道原因,谁能告诉我我的代码有什么问题?

【问题讨论】:

  • 尝试在公共 ViewResolver viewResolver() 方法之上添加@Bean 注解。
  • 非常感谢,先生。它现在正在工作。
  • 不客气。

标签: java spring-mvc


【解决方案1】:

尝试在WebConfig#viewResolver() 方法之上添加@Bean 注解。因此,Spring 容器将您的方法作为 bean 管理,您的自定义配置可能会起作用。

@Bean
public ViewResolver viewResolver(){}

表示一个方法产生一个由 Spring 管理的 bean 容器。

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 2021-12-18
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2015-12-02
    • 2021-04-06
    相关资源
    最近更新 更多