Digging in Java

          maven, spring, hibernate, et al

          2011年4月7日

          在前面的第二部分搭了個(gè)spring的架子,把spring-webmvc需要的東西都準(zhǔn)備好了
          先來(lái)把這個(gè)web app補(bǔ)充一下吧,就是一個(gè)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的,版本號(hào)不對(duì),以后會(huì)有問(wèn)題
          除了servlet之外,web.xml里還有一些參數(shù):
          contextConfigLocation定義了spring web的配置文件,而listener則表示由spring的ContextLoaderListener來(lái)加載這個(gè)配置文件

          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>

          這里定義了一個(gè)spring的view resolver,這個(gè)view resolver就是按照MVC的框架,來(lái)找到對(duì)應(yīng)的view的。InternalResourceViewResolver將會(huì)根據(jù)URL來(lái)決定顯示哪個(gè)jsp。這些jsp文件都在 WEB-INF目錄下。

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

          maven 在建項(xiàng)目的時(shí)候,在src/main/webapp下自動(dòng)生成了一個(gè)index.jsp,可以把它移到 WEB-INF目錄下

          下一步,再打開(kāi) pom.xml,把這段加進(jìn)去:
              <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>
          這里定義了兩個(gè)plug-in,一個(gè)很明顯,意思是用java 1.6版本來(lái)做編譯。而另一個(gè)則是maven的jetty插件,jetty是一個(gè)web server。

          好了,再build一次
          mvn clean install
          然后運(yùn)行命令
          mvn jetty:run
          就是啟動(dòng)一個(gè)jetty,并且把本項(xiàng)目deploy。啟動(dòng)完成后,就可以用瀏覽器打開(kāi)
          http://localhost:8080/my-webapp/my/





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

          運(yùn)行 mvn eclipse:eclipse,然后把項(xiàng)目導(dǎo)入eclipse里。在eclipse里打開(kāi)pom.xml,這是maven的配置文件。如果裝了maven插件,可以看到pom.xml的編輯器里有Overview, Dependencies, Plugins等tab頁(yè)。
          maven的eclipse plugin可以在 http://m2eclipse.sonatype.org/installing-m2eclipse.html 找到

          既然要用到spring web MVC,那當(dāng)然要有spring的web mvc jar,而web mvc又需要spring web,它們還要用到spring core, context,和beans, 這叫作dependency。而這些spring的庫(kù)之外,還需要commons-loggin等等。沒(méi)有maven的時(shí)候,就不得不搜索,下載,導(dǎo)入.....才能讓程序編譯。而在maven里,maven會(huì)管理這些dependencies。比如說(shuō),目前需要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>
          運(yùn)行一下mvn eclipse:clean eclipse:eclipse,然后刷新eclipse,可以看到所需要的庫(kù)文件已經(jīng)下載并且加在eclipse的classpath里了

          maven會(huì)在當(dāng)前用戶的home目錄下建一個(gè) .m2/repository 目錄,然后把下載下來(lái)的jar文件放在里面。windows下home目錄是在Document and Setting,而linux就在用戶自己的home目錄

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


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

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 赣州市| 南雄市| 同德县| 望谟县| 法库县| 余干县| 桃园市| 涿州市| 旌德县| 兴山县| 景洪市| 黄骅市| 台安县| 嵊州市| 枞阳县| 桐城市| 林口县| 临沂市| 登封市| 广丰县| 沽源县| 屏东市| 沭阳县| 抚宁县| 藁城市| 什邡市| 漠河县| 沧源| 民乐县| 澄迈县| 土默特左旗| 施甸县| 永和县| 韩城市| 信丰县| 新泰市| 隆子县| 武川县| 泰州市| 旌德县| 成武县|