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 閱讀(398) | 評論 (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 閱讀(1700) | 評論 (1)編輯 收藏

          導航

          <2011年4月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 方山县| 墨脱县| 池州市| 金塔县| 会东县| 开封县| 志丹县| 瓦房店市| 桃园县| 永和县| 南平市| 兖州市| 中山市| 万宁市| 柳州市| 梅州市| 揭阳市| 尉氏县| 南阳市| 明水县| 宕昌县| 会泽县| 金门县| 苏尼特右旗| 成都市| 高邮市| 扎囊县| 偃师市| 前郭尔| 苍南县| 遵义市| 虎林市| 淮滨县| 克拉玛依市| 西宁市| 绥滨县| 平阳县| 茌平县| 慈利县| 大悟县| 广元市|