Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@RequestMapping,404错误_Java_Spring_Spring Mvc_Tomcat_Http Status Code 404 - Fatal编程技术网

Java Spring@RequestMapping,404错误

Java Spring@RequestMapping,404错误,java,spring,spring-mvc,tomcat,http-status-code-404,Java,Spring,Spring Mvc,Tomcat,Http Status Code 404,所以,我试图学习Spring框架,但目前我被困在这个问题上,由于某些原因,我的映射无法工作,当我尝试访问那个地址时,我得到了一个404错误 有人能澄清我做错了什么吗(无论是在spring类的使用方面还是在我上面描述的问题的根源方面) 我正在使用本视频中所示的示例配置spring,代码基本相同,唯一的区别是包名和JSP文件的位置(这可能是我的问题的根源吗?) 该项目由IntelliJ Idea制作,并连接到以下链接(Google Drive): 作为我的servlet容器,我使用Tomcat8(尝

所以,我试图学习Spring框架,但目前我被困在这个问题上,由于某些原因,我的映射无法工作,当我尝试访问那个地址时,我得到了一个404错误

有人能澄清我做错了什么吗(无论是在spring类的使用方面还是在我上面描述的问题的根源方面)

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

该项目由IntelliJ Idea制作,并连接到以下链接(Google Drive):

作为我的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";
    }
}
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[]{"/"};
    }
}
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;
    }

}
WebInit.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";
    }
}
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[]{"/"};
    }
}
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.jsppath:/web/home.jsp

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

  • 将war插件添加到pom.xml
  • 修复web资源目录路径(was webapp)

  • 你应该把密码放在这里。哎呀,对不起,忘了。DoneSo,我忘了在pom.xml中添加war,但它仍然没有解决我的问题,它确实映射URL,但我仍然得到404错误。它显示HTTP状态404-/home.jsp,因此出于某种原因,它无法找到my view aka home.jspHow来添加war插件: