flio

             :: 首頁(yè) :: 聯(lián)系 ::  :: 管理
            0 Posts :: 6 Stories :: 6 Comments :: 0 Trackbacks
          這段時(shí)間開(kāi)始封裝公司內(nèi)部使用的開(kāi)發(fā)框架,基于性能的考慮,使用srpingmvc+hibernate技術(shù),下面是一些搭建過(guò)程(不包含dao層封裝),寫(xiě)下來(lái)備忘。
          1.讓springmvc跑起來(lái)
          這個(gè)過(guò)程很簡(jiǎn)單。導(dǎo)入spring的jar包就好了,這里簡(jiǎn)單的介紹下jar包吧。主要分srping的實(shí)現(xiàn)包,和依賴(lài)包
          主包:spring-framework-3.1.1.RELEASE-with-docs.zip
          依賴(lài):spring-framework-3.0.1.RELEASE-A-dependencies.zip
          主包中不需要全部加進(jìn)去,選一些需要的就好了如下:



          然后再加入依賴(lài):


          加入了jar包后就開(kāi)始配置web.xml文件,在web.xml中加入如下代碼。
          <!--初始化srpingMVC-->
            
          <servlet>
             
          <servlet-name>test</servlet-name>
             
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             
          <!-- 這樣可以配置xml-servlet,不默認(rèn)找test-servlet.xml -->
             
          <init-param>
              
          <param-name>contextConfigLocation</param-name>
              
          <param-value>classpath:/configSource/test-servlet.xml</param-value>
             
          </init-param>
             
          <load-on-startup>1</load-on-startup>
          </servlet>
            
          <servlet-mapping>
             
          <servlet-name>test</servlet-name>
             
          <!--這里我是攔截以.test結(jié)尾的請(qǐng)求-->
             
          <url-pattern>*.test</url-pattern>
            
          </servlet-mapping>

          因?yàn)閟rpingmvc是基于servlet實(shí)現(xiàn)的。所有請(qǐng)求都需要通過(guò)DispatcherServlet來(lái)轉(zhuǎn)發(fā)請(qǐng)求
          解釋下init-param這段,默認(rèn)可以不需要配置的,只要在WEB-INF下面定義test-servlet.xml就可以自己找到的,(test-servlet.xml,這個(gè)名字的由來(lái)是因?yàn)槲业膕ervlet的name是test,所有有test-servlet.xml文件就會(huì)自動(dòng)找到這個(gè)文件,紅色字體標(biāo)注),這樣的話下面的init-param這一個(gè)節(jié)點(diǎn)就可以不需要配置了。
          但是為了管理方便,我放在了configSource這個(gè)包里面,所以我在param-value中的<param-value>classpath:/configSource/*-servlet.xml</param-value>是這樣配置的。基本這樣就配置完了,可以簡(jiǎn)單測(cè)試,就來(lái)個(gè)helloworld例子吧。
          在test-servlet.xml中配置如下代碼:
           1<beans xmlns="http://www.springframework.org/schema/beans"
           2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           3 xmlns:context="http://www.springframework.org/schema/context"
           4 xmlns:mvc="http://www.springframework.org/schema/mvc"
           5 xsi:schemaLocation="http://www.springframework.org/schema/beans   
           6           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           7           http://www.springframework.org/schema/mvc  
           8           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           9           http://www.springframework.org/schema/context   
          10           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          11 <!-- 自動(dòng)掃描com.baobaotao.web 包下的@Controller標(biāo)注的類(lèi)控制器類(lèi) -->
          12 <context:component-scan base-package="com.Integrat.*.controller" />
          13 <!-- 啟動(dòng)Spring MVC的注解功能,完成請(qǐng)求和注解POJO的映射 -->
          14 <mvc:annotation-driven/>
          15</beans>
          16

          重點(diǎn)只有兩句,就是開(kāi)啟注解,和自動(dòng)掃描控制器
          base-package="com.Integrat.*.controller"  這個(gè)應(yīng)該懂事這么意思了吧,不解釋。
          配置好這些了就開(kāi)始寫(xiě)controller吧
          代碼如下:

           

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.springframework.web.bind.annotation.RequestMapping;

          @org.springframework.stereotype.Controller   
          //這里是注解,表示這是一個(gè)控制器
          @RequestMapping("/helloworld")                    //這里是訪問(wèn)路徑,個(gè)人感覺(jué)和struts的namespace一點(diǎn)類(lèi)似,不過(guò)功能不止如此,略過(guò)
          public class HelloWorldController{
               @RequestMapping(
          "/hello")                      //注意這里也寫(xiě)個(gè)
               public String query(){
                        System.out.println(
          "hello world!!!");
                        
          return null;
               }

          }



          這樣基本就可以了。發(fā)布項(xiàng)目,訪問(wèn)地址-->  http://localhost:8080/項(xiàng)目名/helloworld/hello.test
          就可以看到控制臺(tái)輸出helloworld了。
          未完待續(xù)....

          posted on 2012-05-30 13:38 flio 閱讀(585) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): 框架技術(shù)

          Feedback

          # re: SpringMVC+hibernate3(1.讓springmvc跑起來(lái)) 2012-05-31 08:30 x7
          哥頂了  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 杂多县| 阳春市| 青冈县| 遂平县| 赣榆县| 那坡县| 遵化市| 青田县| 社旗县| 谢通门县| 额济纳旗| 莲花县| 大同县| 汾阳市| 永安市| 阿拉善右旗| 日土县| 区。| 镇雄县| 京山县| 顺平县| 岫岩| 南乐县| 徐州市| 当雄县| 新乡市| 无极县| 十堰市| 延川县| 凌海市| 昌平区| 信丰县| 遂宁市| 手游| 德昌县| 黄陵县| 泸水县| 永吉县| 仪征市| 无极县| 赞皇县|