java - 从intellij上的tomcat获取错误404

标签 java spring tomcat intellij-idea

我正在尝试在 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/

相关文章:

java - 为什么我们使用 Lifecycle 感知组件以及 Android 中 Lifecycle Observer 的用途是什么?

eclipse - java.lang.ClassNotFoundException : org. springframework.core.NestedRuntimeException 异常

java - JSP 文件中的 Lambda 表达式无法编译

apache - Tomcat、Mod_rewrite、Mod_proxy : how do I keep the original URI path in a proxied request?

java - Java 中使用 HTTPClient 的 GZip POST 请求

Java/JSP log4j :WARN Please initialize the log4j system properly

java - 在 Java 中将 unix 时间戳转换为日期时间

java - linux下spring ResourceServlet在jetty和tomcat中抛出too much open files异常

java - FlatMap 何时会同时监听多个源?

html - JSP - 将数据从 Controller 传输到 Scriptlet