4

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

Community
  • 1
  • 1
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • 1
    To answer your last question. If you enter the server configuration, the second option box (I'm on my cell, don't recall the name) you can see the home path, usually somewhere in your workspace. You find the log in the usual logs folder. – Mads Nielsen Feb 20 '13 at 05:32
  • Is there any output in the tomcat log/console (text, or exception)? – Ralph Feb 20 '13 at 08:02
  • I've put the outputs from Eclipse and Tomcat at the bottom of my question. Thank you :) – Benjamin M Feb 20 '13 at 13:23
  • had the same issue ... no logs in console ... only 404s ... after changing the deployment assembly properties ... it works ok now – cristi _b Apr 18 '14 at 16:57
  • Please post your solution as an answer and accept it, please. In this way people will see immediately that the question is solved and doesn't need a solution anymore. ;) – bluish Jun 11 '15 at 13:30
  • 1
    @bluish: only for you ;) – Benjamin M Jun 11 '15 at 13:39

1 Answers1

6

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.
bluish
  • 26,356
  • 27
  • 122
  • 180
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
  • It worked for me, but I don't understand why? I have started my project a thousand time and it never happens, but today morning it stopped and your solution worked. – Himanshu Bhandari Jul 04 '16 at 06:38
  • I am glad I found this thread. Maven dependencies were missing in WEB-INF/lib folder. Adding Maven Dependencies in Java Build Path Entries fixed the problem. – vinay Nov 14 '16 at 18:52