在intellij上从tomcat获取错误404

时间:2017-04-14 21:41:30

标签: java spring tomcat intellij-idea

我试图在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
}
}

我希望这足以帮助解决我的问题。谢谢!

编辑:我在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中删除了插件之后)

1 个答案:

答案 0 :(得分:0)

尝试将InternalResourceViewResolver的前缀更新到您的观看文件夹:

  resolver.setPrefix("/WEB-INF/views/");

您需要在视图文件夹中包含hey.jsp