简体   繁体   English

从Eclipse中运行Tomcat总是可以得到404(但是index.html可以工作)

[英]Running Tomcat from within Eclipse always gives me 404 (but index.html works)

first my software stack: 首先是我的软件堆栈:

  • Eclipse 4.2 Eclipse 4.2
  • Tomcat 7.0.37 Tomcat 7.0.37
  • Maven with m2e plugin Maven与M2E插件
  • javax.servlet 3.0.1 javax.servlet 3.0.1
  • Spring WebMVC and Spring Web 3.2.1 Spring WebMVC和Spring Web 3.2.1

I'm using servlet 3 without web.xml . 我正在使用没有web.xml的 servlet 3。

What I did: 我做了什么:

  • Eclipse -> New Maven Project Eclipse->新Maven项目
  • edit pom.xml: add Spring and javax.servlet 编辑pom.xml:添加Spring和javax.servlet
  • Eclipse -> Project properties -> Project facets -> add dynamic web facet Eclipse->项目属性->项目构面->添加动态Web构面
  • Eclipse -> Project properties -> Deployment Assembly -> Remove WebContent and add /src/main/webapp Eclipse->项目属性->部署程序集->删除WebContent并添加/ src / main / webapp
  • added javax.servlet.ServletContainerInitializer to /src/main/webapp/META-INF/services and put inside the fully qualified class name which implements WebApplicationInitializer 在/ src / main / webapp / META-INF / services中添加了javax.servlet.ServletContainerInitializer ,并将其放入实现WebApplicationInitializer的完全限定的类名中

At the moment my project contains about 7 files (3x .java, 1x .jsp, 1x .html, 1x .pom, 1x javax.servlet.ServletContainerInitializer) 目前,我的项目包含约7个文件(3x .java,1x .jsp,1x .html,1x .pom,1x javax.servlet.ServletContainerInitializer)

What works: 什么有效:

Compiling and war-packaging via Maven and then simply deploy the .war on a standalone Tomcat. 通过Maven进行编译和打包,然后将.war部署在独立的Tomcat上。

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

What doesn't work: 什么不起作用:

If I now try to use Eclipse via Run -> Run on Server . 如果我现在尝试通过Run -> Run on Server使用Eclipse。 Then I choose my Tomcat directory, etc... The servlet ( /hello ) just gives an 404. But the index.html works. 然后,选择我的Tomcat目录,等等。servlet( /hello )仅给出404。但是index.html可以工作。

The 3 Java Files: 3个Java文件:

Initializer.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("/");    
    }
}

WebAppConfig.java: 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;
    }
}

FooController.java: FooController.java:

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

If you need any more additional information, just tell me. 如果您需要更多其他信息,请告诉我。

Thank you!! 谢谢!!

EDIT: Where can I find the log files for Tomcat if it has been started via Eclipse? 编辑:如果已通过Eclipse启动了Tomcat,我在哪里可以找到它的日志文件?

Outputs: 输出:

Eclipse Console (this all comes after starting Tomcat and it doesn't change even if I call some URLs like http://localhost:8080/ or http://localhost:8080/MyApp/ ): http://pastebin.com/gGn0j48T Eclipse控制台(所有这些都是在启动Tomcat之后出现的,即使我调用了一些URL,例如http://localhost:8080/http://localhost:8080/MyApp/ ,它也不会更改 ): http : //pastebin.com / gGn0j48T

Tomcat logs seem to be saved in .metadata/.plugins/org.eclipse.wst.server.core/tmp0/logs/ . Tomcat日志似乎保存在.metadata/.plugins/org.eclipse.wst.server.core/tmp0/logs/ There's just one file localhost_access_log.2013-02-20.txt and the output doesn't look interesting: http://pastebin.com/QmD4wPmA 只有一个文件localhost_access_log.2013-02-20.txt ,输出看起来并不有趣: http : //pastebin.com/QmD4wPmA

Now I've done this to get a catalina.out : https://stackoverflow.com/a/5045247/1321564 现在我这样做是为了获得catalina.outhttps : //stackoverflow.com/a/5045247/1321564

And here's the output after restarting Tomcat and trying some URLs: The file stays empty... ?! 这是重新启动Tomcat并尝试一些URL之后的输出:该文件保持为空...?!

I've also realized that the following directory is empty on Tomcat: wtpwebapps/MyApp/WEB-INF/lib/ . 我还意识到,以下目录在Tomcat上为空: wtpwebapps/MyApp/WEB-INF/lib/ Shouldn't be some Spring libs in there?! 那里不应该有一些Spring库吗?

Got it working!!! 得到它的工作!

As mentioned above: There were no Spring libs in the WEB-INF/lib folder. 如上所述: WEB-INF/lib文件夹中没有Spring库。

And because I'm using Maven. 而且因为我正在使用Maven。 The solution is pretty simple: 解决方案非常简单:

  • Right click the Project 右键单击项目
  • Click Properties 单击Properties
  • Click Deployment Assembly 单击Deployment Assembly
  • Click Add... 点击Add...
  • Choose Java Build Path Entries 选择Java Build Path Entries
  • Click Next 点击Next
  • Choose Maven Dependencies 选择Maven Dependencies
  • Click Finish 点击Finish

Restart the Server. 重新启动服务器。

Now I can see a difference in the Server View: 现在,我可以在服务器视图中看到不同之处:

  • Expand the Tomcat Server. 展开Tomcat服务器。 Now you can see the Project 现在您可以看到项目
  • Expand the Project. 展开项目。 Now there is spring-web-3.2.1.RELEASE.jar listed . 现在列出了spring-web-3.2.1.RELEASE.jar That wasn't the case before. 以前不是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tomcat 404错误,但在Eclipse中有效 - tomcat 404 error, but it works within eclipse index.html不显示我的WebApp(Eclipse中的Tomcat 7) - index.html not displaying for my WebApp (Tomcat 7 in Eclipse) 错误404从Eclipse运行Tomcat - Error 404 running Tomcat from Eclipse Servlet提供404(Eclipse + Tomcat) - Servlet gives 404 (Eclipse + Tomcat) 从Eclipse运行应用程序时在tomcat中获取错误404 - Getting error-404 in tomcat while running application from eclipse Tomcat从命令提示符运行但在eclipse中显示404错误 - Tomcat running from command prompt but shows 404 Error in eclipse Spring Boot / Eclipse index.html:发生意外错误(类型=未找到,状态= 404) - Spring Boot/Eclipse index.html: There was an unexpected error (type=Not Found, status=404) 当WAR文件工作时,Eclipse的Tomcat提供404 - 以前的解决方案不起作用 - Eclipse's Tomcat gives a 404 while the WAR file works-previous solutions don't work 在tomcat 7.0中运行Spring MVC应用程序会给我HTTP Status 404:请求的资源不可用。 - Running a Spring MVC app in tomcat 7.0 gives me HTTP Status 404 : requested resource is not available. Webapp在Eclipse中运行,但是直接从Eclipse之外的Tomcat运行时却出现404错误 - Webapp running in Eclipse, but getting 404 error when running directly from Tomcat outside Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM