Reputation: 24567
first my software stack:
I'm using servlet 3 without web.xml.
What I did:
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.htmlhttp://127.0.0.1/MyApp/hello
shows the hello.jspWhat 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?
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?!
Upvotes: 4
Views: 7883
Reputation: 24567
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:
Properties
Deployment Assembly
Add...
Java Build Path Entries
Next
Maven Dependencies
Finish
Restart the Server.
Now I can see a difference in the Server View:
spring-web-3.2.1.RELEASE.jar
listed. That wasn't the case before.Upvotes: 6