2016-08-31 9 views
2

Spring Web MVC 4.3.2と埋め込みTomcat 7.0.64を使用してWebアプリケーションを構築しようとしています。Spring 4 + Embedded Tomcat 7

埋め込みTomcatを起動する正しいメインメソッドを書き込むことができませんでした。これはSpring Controllerが@ResponseBodyコンテンツ(JSON)を送信してもJSPビューでは失敗しました。 JSPの

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(); 
} 

それは言う表示:私はJSPがあり、絶対パスにappBase変数を設定した場合、要求されたリソースが利用可能でない(WEB-INF /ビュー/ home.jspを)HTTP 404

を、 できます。しかし、もちろん、それは別のマシンでは動作しないため、解決策ではありません。私は相対的な道が必要です。

appBase varibaleを"src/main/webapp"に設定すると、次のエラー: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でTomcatが起動できなくなります。

さらに、Gradle fat jar法で構築された瓶にはWEB-INFディレクトリがありません。

エンベデッドTomcatとJSPで動作する単純なSpring MVCアプリケーション(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ランチャー:

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(); 
    } 

} 

春のWebアプリケーションの初期化子:

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(); 
    } 

} 

フォルダ構造:

enter image description here

+2

なぜこれらの方法をスプリングブーツで個別に使用したのですか? – UserF40

+1

@ UserF40:私は春のブートのような人々の大部分を知っています。個人的には、自分のために決定が下されたことが好きではなく、そのことがどのように起こるのか理解したい。私は春のブートの "魔法"が好きではありません。私は確かに間違っています。しかし、私はここで議論を作成したくないと思う。 – Comencau

+0

ここにある可能性のある事実ポイントを追加してください。http://stackoverflow.com/q/38633023/6544712​​ – UserF40

答えて

0

はどうやら、埋め込まれたTomcatは、静的なリソースがMETA-INF/resourcesディレクトリにあることを期待しています。私はこれに続き:tutorialと私は最後の瓶がどのように構造化されているかを調べた。

そこで、Gradleビルドスクリプトを修正してJSPを配置しました。

sourceSets { 
    main { 
     resources.srcDirs = ["src/main/webapp"] 
     output.resourcesDir = "$buildDir/classes/main/META-INF/resources" 
    } 
} 

これで機能します。しかし、私はそれが一時的な仕事だと感じています。誰かがより満足と教育的な答えを持っているなら、私はそれを得ることを喜ばしく思います。

関連する問題