在tomcat7

时间:2015-08-02 22:03:31

标签: java spring tomcat war build.gradle

我是tomcat的新手。我跑#34; gradle tomcatRunWar"。此Web应用程序可以在本地运行,并且已调用 WebApplicationInitializer 。然后,我使用了#34; gradle war"得到战争档案。我删除了/ var / lib / tomcat7 / webapps /中的所有内容。我将myproject.war复制到/ var / lib / tomcat7 / webapps /目录。将其重命名为ROOT.war。重新启动tomcat7。服务器开始运行。但我无法访问my_ip上的网页:8080 / login或my_ip:8080 / myproject / login。错误是" HTTP状态404 - /登录"。在localhost.log中,有一个信息:“ INFO:在类路径上检测不到Spring WebApplicationInitializer类型”。我通过谷歌搜索了这个问题,因为lib版本,jdk版本,有些人遇到了同样的问题。我不知道如何调试。

Apache Tomcat / 7.0.52, java版" 1.8.0_45" Java(TM)SE运行时环境(版本1.8.0_45-b14)Java HotSpot(TM)64位服务器VM(版本25.45-b02,混合模式)

build.gradle是:

    buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE")
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'groovy'
apply plugin: 'tomcat'


eclipse {
    jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.7
    }
}

jar {
    baseName = 'myproject'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
}


dependencies {
    compile('commons-fileupload:commons-fileupload:1.3.1')
    compile("org.codehaus.jackson:jackson-mapper-asl:1.9.0")
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.slf4j'
    }
    compile("javax.servlet:javax.servlet-api:3.0.1")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    compile("org.springframework.data:spring-data-mongodb")  {
        exclude group: 'org.slf4j'
    }
    compile("org.springframework.security:spring-security-crypto:3.2.3.RELEASE")
    compile("com.fasterxml.jackson.core:jackson-databind")
    compile("joda-time:joda-time:2.2")
    compile("org.apache.directory.studio:org.apache.commons.codec:1.6")
    compile('com.amazonaws:aws-java-sdk:1.2.1')
    testCompile("junit:junit")
        runtime 'javax.servlet:jstl:1.2'
    providedCompile group:"org.apache.tomcat", name:"tomcat-catalina", version:"7.0.52" 
}

dependencies { 
    def tomcatVersion = '7.0.52' 
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", 
    "org.apache.tomcat.embed:tomcat-embed-logging-log4j:${tomcatVersion}" 
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") { 
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj' 
    } 
}

tomcatRun { 
    daemon = true 
    httpPort = 8080 
    outputFile = file('/logs/tomcat.log') 
    stopKey = 'ctl-d'  
}

tomcatRunWar { 
    outputFile = file('/logs/tomcat.log') 
}

tomcatStop { 
    stopKey = 'ctl-d' 
}

/var/lib/tomcat7/logs/catalina.out是:

Aug 03, 2015 12:41:30 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 03, 2015 12:41:30 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1009 ms
Aug 03, 2015 12:41:30 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 03, 2015 12:41:30 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52 (Ubuntu)
Aug 03, 2015 12:41:30 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/examples.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/docs.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/manager.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/host-manager.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/ROOT.war

Aug 03, 2015 12:41:37 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 03, 2015 12:41:37 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 7788 ms

在localhost.log中,有一个信息:“ INFO:在类路径上检测不到Spring WebApplicationInitializer类型

我的Application.java是:

    @Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
      @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }

}

我的WebAppInitializer.java是:

@Configuration
public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {

        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(Application.class);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                "SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

更新

我使用web.xml而不是WebApplicationInitializer。现在它有效。

1 个答案:

答案 0 :(得分:1)

问题在于你的访问目录,你需要验证你的CATALINA_HOME环境变量是什么,日志是否清楚"目录不存在"。

无法从执行位置进行访问。

解: 1.首先,尝试单独启动tomcat而不使用Gradle。 2.在tomcat中部署任何hello world Web应用程序 3.尝试逐步使用Gradle 4.要了解有关Gradle的更多信息,请查看此处: http://examples.javacodegeeks.com/core-java/gradle/gradle-hello-world-tutorial/

问候。