从Eclipse中运行Tomcat总是给我404(但index.html有效)

Ben*_*n M 4 eclipse spring tomcat maven servlet-3.0

首先我的软件堆栈:

  • Eclipse 4.2
  • Tomcat 7.0.37
  • 使用m2e插件的Maven
  • javax.servlet 3.0.1
  • Spring WebMVC和Spring Web 3.2.1

我正在使用没有web.xml的 servlet 3 .

我做了什么:

  • Eclipse - > New Maven项目
  • 编辑pom.xml:添加Spring和javax.servlet
  • Eclipse - >项目属性 - >项目构面 - >添加动态Web构面
  • Eclipse - >项目属性 - >部署程序集 - >删除WebContent并添加/ src/main/webapp
  • 添加javax.servlet.ServletContainerInitializer到/ src/main/webapp/META-INF/services并放入实现的完全限定类名WebApplicationInitializer

目前我的项目包含大约7个文件(3x .java,1x .jsp,1x .html,1x .pom,1x javax.servlet.ServletContainerInitializer)

什么有效:

通过Maven编译和war-packaging,然后简单地在独立的Tomcat上部署.war.

  • http://127.0.0.1/MyApp/ 显示index.html
  • http://127.0.0.1/MyApp/hello 显示hello.jsp

什么行不通:

如果我现在尝试使用Eclipse Run -> Run on Server.然后我选择我的Tomcat目录等... servlet(/hello)只给出了404.但index.html有效.

3个Java文件:

Initializer.java:

public class Initializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");    
    }
}
Run Code Online (Sandbox Code Playgroud)

WebAppConfig.java:

@Configuration
@ComponentScan("myapp.server")
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
Run Code Online (Sandbox Code Playgroud)

FooController.java:

@Controller
public class FooController { 
    @RequestMapping("/hello")
    public String helloWorld(Model model) {
        model.addAttribute("wisdom", "Goodbye XML");
        return "hello";
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您还需要更多信息,请告诉我.

谢谢!!

编辑:如果已经通过Eclipse启动Tomcat的日志文件,我在哪里可以找到它?

输出:

Eclipse控制台(这一切都是在启动Tomcat之后出现的,即使我将一些URL称为或者它也不会改变):http://pastebin.com/gGn0j48Thttp://localhost:8080/http://localhost:8080/MyApp/

Tomcat日志似乎已保存在.metadata/.plugins/org.eclipse.wst.server.core/tmp0/logs/.只有一个文件localhost_access_log.2013-02-20.txt,输出看起来不是很有趣:http://pastebin.com/QmD4wPmA

现在我已经这样做了catalina.out:https://stackoverflow.com/a/5045247/1321564

这是重启Tomcat并尝试一些URL后的输出:文件保持空白......?!

我也意识到Tomcat上的以下目录是空的:wtpwebapps/MyApp/WEB-INF/lib/.不应该有一些Spring库吗?!

Ben*_*n M 6

搞定了!!!

如上所述:WEB-INF/lib文件夹中没有Spring库.

因为我正在使用Maven.解决方案非常简单:

  • 右键单击Project
  • 点击 Properties
  • 点击 Deployment Assembly
  • 点击 Add...
  • 选择 Java Build Path Entries
  • 点击 Next
  • 选择 Maven Dependencies
  • 点击 Finish

重启服务器.

现在,我可以看到服务器视图的不同之处:

  • 展开Tomcat服务器.现在您可以看到该项目
  • 展开项目.现在spring-web-3.2.1.RELEASE.jar列出了.事实并非如此.