所以,我试图学习Spring框架,但目前我被困在这个问题上,出于某种原因,我的映射不起作用,当我尝试访问该地址时,我得到一个404错误。Spring @RequestMapping,404错误
有人可以澄清我做错了什么(都在春季班的使用和我上面描述的问题的来源)?
我使用本视频中显示的示例配置spring:YouTube Video,代码基本相同,唯一的区别是包名和JSP文件的位置(这可能是我的问题的根源?) 。
该项目于IntelliJ IDEA的制作,并连接到下面的链接(谷歌驱动器):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 path:/web/home.jsp
你应该把你的代码在这里。 – viruskimera
哎呀,对不起,忘了。完成 –
因此,我忘了在我的pom.xml中添加 war ,但它仍然没有解决我的问题,它确实映射了url,但仍然收到404错误。它说HTTP状态404 -/home.jsp所以由于某种原因,它无法找到我的看法又名home.jsp –