Hello world Spring MVC Web应用程序中的Http 404错误完全由java配置

时间:2017-03-09 14:04:52

标签: java spring spring-mvc

我是Spring Web MVC的新手,我尝试尝试Hello world app。

我跟随了一个例子来自" Spring In Action 4th ed"本书只使用java配置(没有任何XML文件)。我使用Tomcat v7.0服务器和servlet 3。

问题是当我尝试进入主页时遇到Http 404错误。

使用过的Java类是:

DispatcherServlet配置:

package config;

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

public class FirstWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

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

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

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

Spring MVC配置:

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

@Configuration
@EnableWebMvc
@ComponentScan("web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    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();
    }
}

用于获取其他Beans的配置(在hello world应用程序中没有做任何事情)

package 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={"lgmi_cr"},excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {

}

主页控制器

package web;

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

@Controller
public class HomeController {

    @RequestMapping(value="/")
    public String home() {
        return "home";

    }

}

一个JSP文件&#34; home&#34; uneder / WEB-INF / views /

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>First app</title>
    </head>
    <body>
        <h1>Hello world</h1>
    </body>
</html> 

由于404错误,我的第一印象是它找不到任何控制器,但我不知道为什么。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我刚刚发现了这个问题。

实际上,我将外部Spring jar文件添加到项目类路径中,我忘记将它们添加到myProject / WebContent / WEB-INF / lib。