Spring@RequestMapping,404错误

Spring @RequestMapping, 404 error

所以,我正在尝试学习 Spring 框架,但目前我遇到了这个问题,由于某种原因我的映射不起作用,当我尝试访问该地址时我出现 404 错误。

有人可以澄清我做错了什么吗(在 spring 类 的用法和我上面描述的问题的根源)?

我正在使用此视频中显示的示例配置 spring:YouTube Video,代码基本相同,唯一的区别是包名称和 JSP 文件(这可能是我问题的根源吗?)。

该项目是在IntelliJ Idea中制作的,并附在以下link(Google驱动器):Project Link

作为我的 servlet 容器,我使用 Tomcat8(尝试了 9 和 8.5,没有雪茄)。

HomeController.java

package com.demo.spring.controller;

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

package com.demo.spring.controller;

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

/**
 * Created by ARTERC on 1/16/2017.
 */
@Controller
public class HomeController {
    @RequestMapping("/")
    public String home() {
        return "home";
    }
}

WebInit.java

package com.demo.spring.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

WebConfig.java

package com.demo.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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("com.demo.spring")
public class WebConfig extends WebMvcConfigurerAdapter{

    @Bean
    public InternalResourceViewResolver resolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

home.jsp路径:/web/home.jsp

好的,所以我找到了解决方案。

  1. 将 war 插件添加到 pom.xml
  2. 修复网络资源目录路径(是 webapp)Image Link