Spring MVC-非常简单的Web应用返回404错误

时间:2018-10-24 13:05:18

标签: spring spring-mvc http-status-code-404

我有一个非常令人沮丧的问题。我有一个Web应用程序,当然是一个使用Spring MVC框架的Maven项目(只想在此提及此应用程序应配置有注释,我不想使用XML,只是想看看它是如何工作的) 。当我尝试输入// localhost:8080 / hello时,发生404错误。当我输入//localhost:8080/hello.html时,将查看简单且空的html(hello.html确实很简单-在主体中,我刚刚读取了位于模型中的变量(由HelloController输入)。 // localhost:8080的页面是有效的索引页(index.html)。我正在使用Tomcat 8.5.34服务器。有人知道,那可能是个问题吗?我花了整整一天的时间才发现问题,但是我真的不知道,现在该怎么办。我一直在Google和这里寻找解决方案,但是我什么也没找到。在此先感谢您提供的所有有帮助的答案!

下面我列出了所有项目文件的内容:

配置类-SpringConfig.java

    package com.smc.config;

import com.smc.controllers.HelloController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.smc"})
public class SpringConfig implements WebMvcConfigurer {

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

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean =
                new InternalResourceViewResolver();
        bean.setPrefix("/web/");
        bean.setSuffix(".html");
        return bean;
    }

    @Bean
    public HelloController helloController(){
        return new HelloController();
    }
    //Of course there are empty implementations of the rest methods from interface
        }

WebApplicationInitializer接口的实现-创建调度程序servlet的ApplicationStarter.java(我认为):

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class ApplicationStarter implements WebApplicationInitializer {


    public void onStartup(ServletContext sc) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = getContext();

        ctx.refresh();
        ctx.setServletContext(sc);

        sc.addListener(new ContextLoaderListener(ctx));


        ServletRegistration.Dynamic appServlet = sc.addServlet("DispatcherServlet", new DispatcherServlet(ctx));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("*.html");
    }

    private AnnotationConfigWebApplicationContext getContext(){
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(com.smc.config.SpringConfig.class);
        return ctx;
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>SystemMaintainContracts</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

    </dependencies>


</project>

简单控制器-HelloController.java

package com.smc.controllers;

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

@Controller
public class HelloController {

    @RequestMapping(value = "/hello")
    public String showIndexPage(Model model){
        model.addAttribute("hello", "hello from HelloController!");
        return "hello";
    }
}

这是我的项目结构: project structure

1 个答案:

答案 0 :(得分:0)

DispatcherServlet配置为仅回答与* .html匹配的请求。这就是为什么要提供静态文件(index.html,hello.html)的原因。

hello控制器已映射到/ hello,与调度程序servlet映射不匹配。

请考虑调度程序的简单/作为映射。这将匹配两个请求。