Digging in Java

          maven, spring, hibernate, et al

          2011年4月7日

          在前面的第二部分搭了個spring的架子,把spring-webmvc需要的東西都準備好了
          先來把這個web app補充一下吧,就是一個hello world

          首先修改 src/main/webapp/WEB-INF目錄下的 web.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation
          ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
              
          <display-name>my Web Application</display-name>

              
          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>/WEB-INF/my-servlet.xml</param-value>
              
          </context-param>

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

              
          <servlet>
                  
          <servlet-name>my</servlet-name>
                  
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              
          </servlet>

              
          <servlet-mapping>
                  
          <servlet-name>my</servlet-name>
                  
          <url-pattern>/my/*</url-pattern>
              </servlet-mapping>

          </web-app>
          這里,web-app的版本是2.5,早期的版本是不支持EL的,版本號不對,以后會有問題
          除了servlet之外,web.xml里還有一些參數:
          contextConfigLocation定義了spring web的配置文件,而listener則表示由spring的ContextLoaderListener來加載這個配置文件

          my-servlet.xml則如下例:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:p
          ="http://www.springframework.org/schema/p"  
              xmlns:context
          ="http://www.springframework.org/schema/context"
              xmlns:mvc
          ="http://www.springframework.org/schema/mvc"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                                  http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

              
          <bean
                  
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  
          <property name="prefix" value="/WEB-INF/jsp/" />
                  
          <property name="suffix" value=".jsp" />
              
          </bean>
              
              
          <mvc:view-controller path="/" view-name="index"/>
                  
          </beans>

          這里定義了一個spring的view resolver,這個view resolver就是按照MVC的框架,來找到對應的view的。InternalResourceViewResolver將會根據URL來決定顯示哪個jsp。這些jsp文件都在 WEB-INF目錄下。

          <mvc:view-controller path="/" view-name="index"/>
          則是直接把根指向了index,通過view resolver,它其實是指向了 WEB-INF/index.jsp

          maven 在建項目的時候,在src/main/webapp下自動生成了一個index.jsp,可以把它移到 WEB-INF目錄下

          下一步,再打開 pom.xml,把這段加進去:
              <build>
                  
          <finalName>my</finalName>
                  
          <plugins>
                      
          <plugin>
                          
          <groupId>org.mortbay.jetty</groupId>
                          
          <artifactId>maven-jetty-plugin</artifactId>
                          
          <version>6.1.26</version>
                      
          </plugin>
                      
          <plugin>
                          
          <artifactId>maven-compiler-plugin</artifactId>
                          
          <configuration>
                              
          <source>1.6</source>
                              
          <target>1.6</target>
                          
          </configuration>
                      
          </plugin>
                  
          </plugins>
              
          </build>
          這里定義了兩個plug-in,一個很明顯,意思是用java 1.6版本來做編譯。而另一個則是maven的jetty插件,jetty是一個web server。

          好了,再build一次
          mvn clean install
          然后運行命令
          mvn jetty:run
          就是啟動一個jetty,并且把本項目deploy。啟動完成后,就可以用瀏覽器打開
          http://localhost:8080/my-webapp/my/





          posted @ 2011-04-07 11:24 GX 閱讀(396) | 評論 (0)編輯 收藏
          概述是挺沒意思的,還是寫代碼做項目有趣。那么來建一個webapp吧
          Maven命令是這樣的:
          mvn archetype:create -DgroupId=com.sky.birds -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
          它會在當前目錄下建一個my-webapp目錄。目錄下有pom.xml和src
          在src/main目錄下手工建一個java目錄,這里可以放java代碼,不明白為什么maven沒有自動把它建起來

          運行 mvn eclipse:eclipse,然后把項目導入eclipse里。在eclipse里打開pom.xml,這是maven的配置文件。如果裝了maven插件,可以看到pom.xml的編輯器里有Overview, Dependencies, Plugins等tab頁。
          maven的eclipse plugin可以在 http://m2eclipse.sonatype.org/installing-m2eclipse.html 找到

          既然要用到spring web MVC,那當然要有spring的web mvc jar,而web mvc又需要spring web,它們還要用到spring core, context,和beans, 這叫作dependency。而這些spring的庫之外,還需要commons-loggin等等。沒有maven的時候,就不得不搜索,下載,導入.....才能讓程序編譯。而在maven里,maven會管理這些dependencies。比如說,目前需要spring webmvc,那么就只需要把spring webmvc加到pom.xml的dependencies里
            <dependencies>
              
          <dependency>
                  
          <groupId>org.springframework</groupId>
                  
          <artifactId>spring-webmvc</artifactId>
                  
          <version>3.0.5.RELEASE</version>
                  
          <type>jar</type>
                  
          <scope>compile</scope>
              
          </dependency>

              .

            
          </dependencies>
          運行一下mvn eclipse:clean eclipse:eclipse,然后刷新eclipse,可以看到所需要的庫文件已經下載并且加在eclipse的classpath里了

          maven會在當前用戶的home目錄下建一個 .m2/repository 目錄,然后把下載下來的jar文件放在里面。windows下home目錄是在Document and Setting,而linux就在用戶自己的home目錄

          repository下的目錄結構是這樣的:
          /<groupId>/<artifactId>/<version>/
          比如說,在maven里配置了hibernate 3.2.7.ga,那么在repository下就是
          org/hibernate/hibernate/3.2.7.ga
          jar文件就放在這個目錄下


          posted @ 2011-04-07 08:43 GX 閱讀(1698) | 評論 (1)編輯 收藏

          2011年4月5日

          Maven是一種project management工具。大致上來說,可以認為它是沒有圖形界面的IDE。你可以用它來編譯,打包,做成jar,war,ear.....

          下載安裝之后,可以通過命令創建一個簡單的java項目

          mvn archetype:create -DgroupId=com.sky -DartifactId=birds -DpackageName=com.sky.birds

          這里,archetype是maven的一個plugin,而create是這個plugin的一個goal

          Maven一個麻煩的地方就是它有自己的目錄layout,大致是這樣的:
          birds/pom.xml
               
          /src/
               
          /src/main/
                   
          /main/java/
                        
          /java/com/sky/birds
               
          /src/test/
                   
          /test/java/

          上面的命令會在com.sky.birds下面生成一個App.java,里面就是一個簡單的hello world程序

          有了這個基本的框架之后,可以用
          mvn install
          來編譯,并且打包成一個jar

          這里install是maven的一個lifecycle phase。maven的lifecycle phase包括process-resources, compile, process-classes, process-test-resources,
          test-compile, test, prepare-package, package......
          在maven的默認設置中,每個phase都附加了相應的plugin goal。而執行一個phase,意味著同時執行所有在它之前的phases

          比方說,process-resources這個lifecycle phase,它對應的plugin goal是resources:resources;test,對應著 surefire:test;package,對應 jar:jar

          當你執行 mvn install的時候,換成plugin就是
          mvn resources:resources \
              compiler:compile \
              resources:testResources \
              compiler:testCompile \
              surefire:test \
              jar:jar



          編譯好了應該可以運行那個hello world了,是這樣的:
          mvn exec:java -Dexec.mainClass=com.sky.birds.App
          要加參數?用 -Dexec.args=xxxx


          項目建起來了,但真正寫代碼還是在eclipse里。把它import到eclipse里?杯具發生了:eclipse不認為那是一個java項目......

          有個命令可以解決這個問題
          mvn eclipse:eclipse

          然后就可以把它作為一個existing project,import到eclipse里了


          順便說一下,建一個web項目類似于
          mvn archetype:create -DgroupId=com.mycompany.app \
                               
          -DartifactId=my-webapp \
                               
          -DarchetypeArtifactId=maven-archetype-webapp


          posted @ 2011-04-05 11:18 GX 閱讀(512) | 評論 (0)編輯 收藏
          僅列出標題  

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 沁水县| 探索| 武乡县| 健康| 青海省| 长泰县| 高平市| 龙泉市| 林州市| 凌云县| 府谷县| 疏附县| 靖州| 玛曲县| 贞丰县| 蒙阴县| 威海市| 胶南市| 中西区| 石景山区| 无为县| 鞍山市| 那曲县| 邹平县| 天津市| 靖安县| 香格里拉县| 新平| 甘肃省| 胶州市| 武功县| 博野县| 诏安县| 宣武区| 都昌县| 若尔盖县| 阆中市| 从江县| 富顺县| 保康县| 甘谷县|