flio

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



          然后再加入依賴:


          加入了jar包后就開始配置web.xml文件,在web.xml中加入如下代碼。
          <!--初始化srpingMVC-->
            
          <servlet>
             
          <servlet-name>test</servlet-name>
             
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             
          <!-- 這樣可以配置xml-servlet,不默認找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結尾的請求-->
             
          <url-pattern>*.test</url-pattern>
            
          </servlet-mapping>

          因為srpingmvc是基于servlet實現的。所有請求都需要通過DispatcherServlet來轉發請求
          解釋下init-param這段,默認可以不需要配置的,只要在WEB-INF下面定義test-servlet.xml就可以自己找到的,(test-servlet.xml,這個名字的由來是因為我的servlet的name是test,所有有test-servlet.xml文件就會自動找到這個文件,紅色字體標注),這樣的話下面的init-param這一個節點就可以不需要配置了。
          但是為了管理方便,我放在了configSource這個包里面,所以我在param-value中的<param-value>classpath:/configSource/*-servlet.xml</param-value>是這樣配置的。基本這樣就配置完了,可以簡單測試,就來個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 <!-- 自動掃描com.baobaotao.web 包下的@Controller標注的類控制器類 -->
          12 <context:component-scan base-package="com.Integrat.*.controller" />
          13 <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->
          14 <mvc:annotation-driven/>
          15</beans>
          16

          重點只有兩句,就是開啟注解,和自動掃描控制器
          base-package="com.Integrat.*.controller"  這個應該懂事這么意思了吧,不解釋。
          配置好這些了就開始寫controller吧
          代碼如下:

           

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

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

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

          }



          這樣基本就可以了。發布項目,訪問地址-->  http://localhost:8080/項目名/helloworld/hello.test
          就可以看到控制臺輸出helloworld了。
          未完待續....

          posted on 2012-05-30 13:38 flio 閱讀(583) 評論(1)  編輯  收藏 所屬分類: 框架技術

          Feedback

          # re: SpringMVC+hibernate3(1.讓springmvc跑起來) 2012-05-31 08:30 x7
          哥頂了  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 邢台市| 雅安市| 民丰县| 吐鲁番市| 邛崃市| 本溪市| 慈利县| 怀来县| 宝清县| 舞钢市| 全州县| 紫阳县| 丁青县| 延长县| 广元市| 东兴市| 邻水| 通山县| 临朐县| 昌宁县| 舞阳县| 讷河市| 邻水| 长兴县| 略阳县| 嘉荫县| 佛冈县| 双鸭山市| 彩票| 北流市| 中西区| 广东省| 龙胜| 左云县| 湖口县| 沙洋县| 成都市| 布拖县| 宕昌县| 蓝田县| 甘肃省|