Spring @RequestMapping,404错误 [英] Spring @RequestMapping, 404 error

查看:140
本文介绍了Spring @RequestMapping,404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试学习Spring Framework,但由于某些原因,我一直陷在这个问题上,由于某些原因,我的映射无法正常工作,当我尝试访问该地址时,我遇到了404错误./p>

有人可以澄清我做错了什么(既使用了Spring类,又提到了我上面描述的问题的根源)?

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

该项目是使用IntelliJ Idea制作的,并附加到以下链接(Google云端硬盘):项目链接

作为servlet容器,我使用的是Tomcat8(9和8.5都尝试过,没有雪茄).

HomeController.java

 包com.demo.spring.controller;导入org.springframework.stereotype.Controller;导入org.springframework.web.bind.annotation.RequestMapping;包com.demo.spring.controller;导入org.springframework.stereotype.Controller;导入org.springframework.web.bind.annotation.RequestMapping;/***由ARTERC在1/16/2017创建.*/@控制器公共类HomeController {@RequestMapping("/")公共字符串home(){回家";}} 

WebInit.java

 包com.demo.spring.config;导入org.springframework.context.annotation.Configuration;导入org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;@配置公共类WebInit扩展AbstractAnnotationConfigDispatcherServletInitializer {受保护的类<?> [] getRootConfigClasses(){返回新的Class<?> [] {RootConfig.class};}受保护的类<?> [] getServletConfigClasses(){返回新的Class<?> [] {WebConfig.class};}受保护的String [] getServletMappings(){返回新的String [] {"/"};}} 

WebConfig.java

 包com.demo.spring.config;导入org.springframework.context.annotation.Bean;导入org.springframework.context.annotation.ComponentScan;导入org.springframework.context.annotation.Configuration;导入org.springframework.web.servlet.config.annotation.EnableWebMvc;导入org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;导入org.springframework.web.servlet.view.InternalResourceViewResolver;@配置@EnableWebMvc@ComponentScan("com.demo.spring")公共类WebConfig扩展了WebMvcConfigurerAdapter {@豆角,扁豆public InternalResourceViewResolver resolver(){InternalResourceViewResolver resolver =新的InternalResourceViewResolver();resolver.setPrefix("/");resolver.setSuffix(.jsp");返回解析器;}} 

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

解决方案

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

  1. 将war插件添加到pom.xml
  2. 修复Web资源目录路径(是webapp)图片链接

So, I'm trying to learn Spring Framework, but currently I'm stuck on this problem, for some reason my mappings don't work and when I try to access that address I'm getting a 404 error.

Can someone please clarify what I'm doing wrong (both in usage of spring classes and the source of the problem I have described above)?

I'm configuring spring using the example shown in this video: YouTube Video, the code is basically the same, the only difference is package name and the location of the JSP file (could this be the source of my problem?).

The project was made in IntelliJ Idea and is attached to the following link (Google Drive): Project Link

As my servlet container I'm using Tomcat8 (tried both 9 and 8.5, no cigar).

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 path: /web/home.jsp

解决方案

Okay, so I found the solution.

  1. Add war plugin to pom.xml
  2. Fix web resource directory path (was webapp) Image Link

这篇关于Spring @RequestMapping,404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆