简体   繁体   English

完全Java配置的Hello World Spring MVC Web应用程序中的Http 404错误

[英]Http 404 error in Hello world Spring MVC Web app fully java configured

I'm new to Spring Web MVC and I tried to try Hello world app. 我是Spring Web MVC的新手,因此尝试尝试Hello World应用程序。

I followed an example from "Spring In Action 4th ed" book which only uses java config (without any XML file). 我遵循了《 Spring In Action 4th ed》一书中的一个示例,该示例仅使用java config(没有任何XML文件)。 I use Tomcat v7.0 server and servlet 3. 我使用Tomcat v7.0服务器和Servlet 3。

The problem is when I try to go to the home page I get Http 404 error. 问题是,当我尝试转到主页时,出现Http 404错误。

The used Java classes are : 使用的Java类是:

DispatcherServlet configuration: 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 configuration : 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();
    }
}

Configuration used to get other Beans (doesn't do any thing in the hello world app) 用于获取其他Bean的配置(在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 {

}

Home page controller 主页控制器

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

    }

}

A JSP file "home" uneder /WEB-INF/views/ 一个JSP文件“ home”目录/ 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> 

Since it 404 error, my first impression is that it could not find any controller but I don't know why. 由于出现404错误,我的第一印象是它找不到任何控制器,但我不知道为什么。

Thank you for any help 感谢您的任何帮助

I have just found the problem. 我刚发现问题。

Actually, I added the external Spring jar files to the project classpath and I forget to add them to myProject/WebContent/WEB-INF/lib. 实际上,我将外部Spring jar文件添加到了项目类路径中,而忘记了将它们添加到myProject / WebContent / WEB-INF / lib中。

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

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