我正在尝试在 intellij 中运行 tomcat,在 IDEA 中一切运行正常,但是当我尝试在浏览器中打开相应的页面时,我得到 HTTP Status 404 – Not Found with the Description 源服务器没有找到目标资源的当前表示,或者不愿意透露它的存在。我到处都看了,没有找到答案,我希望你能帮忙。 我的代码如下:
@EnableWebMvc
@Configuration
@ComponentScan({"com.asign.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver resolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
return resolver;
}
}
@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;//new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
@Controller
public class AppController {
@RequestMapping(value = "/")
public String helloWorld(Model model) {
//let’s pass some variables to the view script
model.addAttribute("wisdom", "Goodbye XML");
return "hey"; // renders /WEB-INF/views/hey.jsp
}
}
我希望这足以帮助我解决问题。谢谢!
编辑:我确实在 Localhost 日志中收到以下消息:
INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
对于面临相同问题的任何人:我无法使这个项目工作,但我使用了来自 http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example-annotation/ 它在我的 Intellij 中工作得很好(在我从 pom.xml 中删除插件之后)
最佳答案
尝试将您的 InternalResourceViewResolver
的前缀更新为您的 View 文件夹:
resolver.setPrefix("/WEB-INF/views/");
并且您需要在 views 文件夹中有 hey.jsp
。
关于java - 从intellij上的tomcat获取错误404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419615/