Spring @RequestMapping,404错误

时间:2017-01-16 14:35:02

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

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

有人可以澄清我做错了什么(使用弹簧类和我上面描述的问题的来源)?

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

该项目是在IntelliJ Idea中制作的,并附加到以下链接(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 个答案:

答案 0 :(得分:0)

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

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