我正在尝试在IntelliJ中运行Tomcat,在IDEA中一切都正常运行,但当我尝试在浏览器中打开相应的页面时,我收到了HTTP状态404 - 未找到的错误提示,其中描述信息为:原始服务器未找到目标资源的当前表示形式,或者不愿意披露其存在。我已经搜索了所有地方,但没有找到答案,希望你能帮忙。
我的代码如下:
@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
}
}
我希望这足以帮助我解决问题。谢谢!
编辑:我在本地主机日志中得到了以下信息:
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文件中删除插件之后)。