简体   繁体   中英

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

first my software stack:

  • Eclipse 4.2
  • Tomcat 7.0.37
  • Maven with m2e plugin
  • javax.servlet 3.0.1
  • Spring WebMVC and Spring Web 3.2.1

I'm using servlet 3 without web.xml .

What I did:

  • Eclipse -> New Maven Project
  • edit pom.xml: add Spring and javax.servlet
  • Eclipse -> Project properties -> Project facets -> add dynamic web facet
  • Eclipse -> Project properties -> Deployment Assembly -> Remove WebContent and add /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

At the moment my project contains about 7 files (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.

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

What doesn't work:

If I now try to use Eclipse via Run -> Run on Server . Then I choose my Tomcat directory, etc... The servlet ( /hello ) just gives an 404. But the index.html works.

The 3 Java Files:

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:

@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:

@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?

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

Tomcat logs seem to be saved in .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

Now I've done this to get a catalina.out : https://stackoverflow.com/a/5045247/1321564

And here's the output after restarting Tomcat and trying some URLs: The file stays empty... ?!

I've also realized that the following directory is empty on Tomcat: wtpwebapps/MyApp/WEB-INF/lib/ . Shouldn't be some Spring libs in there?!

Got it working!!!

As mentioned above: There were no Spring libs in the WEB-INF/lib folder.

And because I'm using Maven. The solution is pretty simple:

  • Right click the Project
  • Click Properties
  • Click Deployment Assembly
  • Click Add...
  • Choose Java Build Path Entries
  • Click Next
  • Choose Maven Dependencies
  • Click Finish

Restart the Server.

Now I can see a difference in the Server View:

  • Expand the Tomcat Server. Now you can see the Project
  • Expand the Project. Now there is spring-web-3.2.1.RELEASE.jar listed . That wasn't the case before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM