2012-08-15 18 views
4

我有一個Spring MVC應用程序,我使用休眠,freemarker。它被設置爲一個多Maven項目。我正在使用IntelliJ Ultimate。碼頭不拿起我的彈簧應用程序

碼頭開始很好,但是當我去

http://localhost:8080/ 

它只是輸出我的項目的文件夾,我可以在瀏覽器中查看我的源代碼!

這是目前我的設置:

final Server server = new Server(8080); 

    ProtectionDomain domain = HttpServer.class.getProtectionDomain(); 
    URL location = domain.getCodeSource().getLocation(); 

    WebAppContext webAppContext = new WebAppContext(); 
    webAppContext.setResourceBase(location.toExternalForm()); 
    webAppContext.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml"); 
    webAppContext.setContextPath("/"); 
    webAppContext.setParentLoaderPriority(true); 

    server.setHandler(webAppContext); 

    server.start(); 
    server.join(); 

我的項目佈局(使用INTELLI J)的佈局就像是一個多Maven項目:

/myapp/src/main/java/main.java (this contains the above code to start jetty) 
/myapp/src/main/webapp 
/myapp/src/main/webapp/assets 
/myapp/src/main/webapp/WEB-INF 
/myapp/src/main/webapp/WEB-INF/web-context.xml (spring config file) 
/myapp/src/main/webapp/WEB-INF/web.xml 
/myapp/src/main/webapp/WEB-INF/views/ (parent folder for my freemarker template files) 
/myapp/src/main/webapp/WEB-INF/views/home/index.ftl 

我的web.xml文件是:

<context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>classpath*:log4j.properties</param-value> 
</context-param> 

<servlet> 
    <servlet-name>myapp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/web-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>myapp</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

當我在的IntelliJ(11 ulimitate)運行它,我得到以下的輸出:

2012-08-15 19:17:11,611 [main] INFO org.eclipse.jetty.server.Server - jetty-7.6.2.v20120308 
2012-08-15 19:17:11,886 [main] INFO org.eclipse.jetty.webapp.StandardDescriptorProcessor - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet 
2012-08-15 19:17:11,962 [main] INFO org.eclipse.jetty.server.handler.ContextHandler - started o.e.j.w.WebAppContext{/,file:/Users/me/projects/myapp/myapp-web/target/classes/} 
2012-08-15 19:17:12,021 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started [email protected]:8080 

這顯然是行不通的,因爲當我使用TomcatW¯¯運行/的IntelliJ我得到的東西像冬眠,春天的巨大輸出等

我對Web模塊的pom.xml有:

.. 
<packaging>war</packaging> 
.. 
+0

看看這可能有幫助(它的工作原理) - > http://stackoverflow.com/a/10609507/169277 – ant 2012-08-15 23:25:30

+0

這是生產的權利?不是爲了本地發展?我看到scanInterval,以便看起來像一個開發設置(以防它是不同的?) – Blankman 2012-08-15 23:53:41

+0

是的這是爲developmnet – ant 2012-08-17 22:53:20

回答

4

如果你的目標在生產運行這一點,你已經在使用Maven來構建應用程序我建議你創建實際war包第一:

mvn clean package 

然後,一旦你有你的web應用程序建立了一個新的Jetty服務器,它運行於一個戰爭檔案庫(而不是源代碼)。

由於碼頭manual狀態,應該是這樣的:

Server server = new Server(8080); 

    WebAppContext webapp = new WebAppContext(); 
    webapp.setContextPath("/"); 
    webapp.setWar("path_to_the_war_file/your_app.war"); 
    server.setHandler(webapp); 

    server.start(); 
    server.join(); 
+0

戰爭和jar包的類型有區別嗎? – Blankman 2012-08-30 12:33:14

+2

技術上講,war和jar都是.zip-like文件(它們使用zip壓縮格式)。 Jar是通常形成一個java庫的java類(和一些元數據)的集合,然而戰爭應該是一個Web應用程序存檔。通常,戰爭歸檔包含Web靜態資源,Java類文件,庫(即jar),容器配置文件和元數據。 – 2012-08-30 13:07:02

+0

@Blankman無論如何,你的碼頭能夠運行一個戰爭包裝的應用程序或它的爆炸版本。問題是你將資源庫設置爲你的src directroywebAppContext.setResourceBase(location.toExternalForm()),這是錯誤的。資源庫是一個包含靜態資源的目錄(使用maven結構 - > src/main/webapp)。另外,如果你想在原地運行它,請將IntelliJ配置爲將類編譯爲src/main/webapp/WEB-INF/classes。希望這有助於 – 2012-08-30 14:41:38

2

你可能缺少你的上下文加載器監聽器。

<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
+0

所以這是需要做嵌入式碼頭,但不使用tomcat? (它通過intellij與tomcat運行時起作用) – Blankman 2012-08-16 05:30:48

+0

無論如何它都應該被需要。這是啓動你的彈簧配置。 – Matt 2012-08-16 10:06:45

+0

因此,當我使用IntelliJ的tomcat安裝程序運行應用程序時工作正常,並且我的web.xml中沒有列表程序。 – Blankman 2012-08-20 23:12:44

0

而不是從main.java手動調用碼頭,請參閱Maven的Jetty的插件,在pom.xml,並通過運行mvn jetty:run

啓動應用程序
<build> 
    <plugins> 
    <plugin> 
     <groupId>org.mortbay.jetty</groupId> 
     <artifactId>jetty-maven-plugin</artifactId> 
     <version>8.1.4.v20120524</version> 
    </plugin> 
    </plugin> 
</build> 

相反,如果你的目標是創建一個可執行文件WAR與碼頭的嵌入式副本,你會想辦法任務爲s ○:

final Connector connector = new SelectChannelConnector(); 
    connector.setPort(8080); 

    final Server server = new Server(); 
    server.addConnector(connector); 
    server.setStopAtShutdown(true); 

    final WebAppContext context = new WebAppContext(); 
    context.setContextPath("/"); 
    context.setServer(server); 

    final ProtectionDomain protectionDomain = Main.class.getProtectionDomain(); 
    final URL location = protectionDomain.getCodeSource().getLocation(); 
    context.setWar(location.toExternalForm()); 
    server.addHandler(context); 

    server.start(); 
    server.join(); 
0

你試過設置defaultsDescriptor參數

webAppContext.setDefaultsDescriptor(JETTY_HOME+"/etc/webdefault.xml"); 

JETTY_HOME是安裝了碼頭,你可以找到JETTY_HOME的/ etc/webdefault.xml含有人體必需的設置。

相關問題