I try to build a web app using Spring Web MVC 4.3.2 and embedded Tomcat 7.0.64.
I did not manage to write the correct main method to start embedded Tomcat. It works for Spring Controller sending @ResponseBody
content (JSON) but failed for JSP views.
public static void main(String[] args) throws Exception {
String appBase = ".";// What to put here ?
Tomcat tomcat = new Tomcat();
String contextPath = "";
String port = System.getProperty("server.port");
tomcat.setPort(port == null ? 8080 : Integer.valueOf(port));
tomcat.getHost().setAppBase(appBase);
Context context = tomcat.addWebapp(contextPath, appBase);
// So that it works when in it's launched from IntelliJ or Eclipse
// Also need that a folder named "META-INF" exists in build/classes/main
// https://bz.apache.org/bugzilla/show_bug.cgi?id=52853#c19
((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
tomcat.start();
tomcat.getServer().await();
}
For JSP view it says : The requested resource is not available (WEB-INF/views/home.jsp) HTTP 404
If I set the appBase
variable to the absolute path where the JSPs are, it works. But, of course, it is not a solution as it would not work on another machine. I need a relative path.
If I set appBase
varibale to "src/main/webapp"
, then Tomcat fails to start with the following error : java.lang.IllegalArgumentException: Document base C:\blabla\spring-jsp-embedded-tomcat\tomcat.8080\src\main\webapp\src\main\webapp does not exist or is not a readable directory
.
Morevover, the jar that is built with Gradle fat jar technique does not contain the WEB-INF dir.
How can I do to make a simple Spring MVC app working with an embedded Tomcat and JSPs (to be launched with java -cp path/to/my/jar com.app.Launcher
) ?
build.gradle :
apply plugin: 'java'
sourceCompatibility = 1.7
version = '1.0'
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
repositories {
maven { url "http://repo1.maven.org/maven2" }
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.6.2'
compile 'org.springframework:spring-webmvc:4.3.2.RELEASE'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.0'
compile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'javax.servlet.jsp:jsp-api:2.2'
compile 'javax.servlet:jstl:1.2'
// Embedded Tomcat
// 2 mandatory libs
compile 'org.apache.tomcat.embed:tomcat-embed-core:7.0.64'
compile 'org.apache.tomcat.embed:tomcat-embed-logging-juli:7.0.64'
// To enable JSPs
compile 'org.apache.tomcat.embed:tomcat-embed-jasper:7.0.64'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Tomcat launcher :
public class Launcher {
public static void main(String[] args) throws Exception {
String contextPath = "";
// String appBase = "C:/absolute/path/to/webapp/dir"; // It works but of course I need a relative path
// String appBase = "."; // Works only for Controller sending back ResponseBody (JSON) but fail to find jsp files
String appBase = "src/main/webapp"; // Tomcat does not start properly
Tomcat tomcat = new Tomcat();
String port = System.getProperty("server.port");
tomcat.setPort(port == null ? 8080 : Integer.valueOf(port));
tomcat.getHost().setAppBase(appBase);
Context context = tomcat.addWebapp(contextPath, appBase);
// So that it works when in it's launched from IntelliJ or Eclipse
// Also need that a folder named "META-INF" exists in build/classes/main
// https://bz.apache.org/bugzilla/show_bug.cgi?id=52853#c19
((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
tomcat.start();
tomcat.getServer().await();
}
}
Spring web app initializer :
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}
WebConfig :
@Configuration
@EnableWebMvc
@ComponentScan("com.app")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Folder structure :
Apparently, embedded Tomcat expects static resources to be in a META-INF/resources
directory. I followed this : tutorial and I checked how the final jar was structured.
So I modified the Gradle build script to put the JSPs there.
sourceSets {
main {
resources.srcDirs = ["src/main/webapp"]
output.resourcesDir = "$buildDir/classes/main/META-INF/resources"
}
}
And now it works. However, I have the feeling that it's a makeshift job. If someone has a more satisfying and educational answer, I would be pleased to get it.