Java Spring@RequestMapping,404错误
所以,我试图学习Spring框架,但目前我被困在这个问题上,由于某些原因,我的映射无法工作,当我尝试访问那个地址时,我得到了一个404错误 有人能澄清我做错了什么吗(无论是在spring类的使用方面还是在我上面描述的问题的根源方面) 我正在使用本视频中所示的示例配置spring,代码基本相同,唯一的区别是包名和JSP文件的位置(这可能是我的问题的根源吗?) 该项目由IntelliJ Idea制作,并连接到以下链接(Google Drive): 作为我的servlet容器,我使用Tomcat8(尝试了9和8.5,没有雪茄) HomeController.javaJava 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(尝
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好的,所以我找到了解决方案
你应该把密码放在这里。哎呀,对不起,忘了。DoneSo,我忘了在pom.xml中添加war,但它仍然没有解决我的问题,它确实映射URL,但我仍然得到404错误。它显示HTTP状态404-/home.jsp,因此出于某种原因,它无法找到my view aka home.jspHow来添加war插件: