版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、簡單寫一個搭建Spring MVC3.0的流程(以Spring3.0.5為列),數(shù)據(jù)庫交互使用SpringJdbcTemplate,附件有項目(沒有jar包)。整個項目結(jié)構(gòu)如下圖所示: 1、去官網(wǎng)下載3.0.5所有jar包,所需jar包,見附件圖片,每個jar包得用處如下:org.springframework.aop- 3.0.0.RELEASE-Spring的面向切面編程,提供AOP(面向切面編程)實現(xiàn)org.springframework.asm- 3.0.0.RELEASE-Spring獨立的asm程序,相遇Spring2.5.6的時候需要asmJar
2、包.3.0開始提供他自己獨立的asmJarorg.springframework.aspects- 3.0.0.RELEASE-Spring提供對AspectJ框架的整合org.springframework.beans- 3.0.0.RELEASE-SpringIoC(依賴注入)的基礎(chǔ)實現(xiàn)org.springframework.context.support- 3.0.0.RELEASE-Spring-context的擴展支持,用于MVC方面org.springframework.context- 3.0.0.RELEASE-Spring提供在基礎(chǔ)IoC功能上的擴展服務(wù),此外還提供許多企業(yè)級
3、服務(wù)的支持,如郵件服務(wù)、任務(wù)調(diào)度、JNDI定位、EJB集成、遠(yuǎn)程訪問、緩存以及各種視圖層框架的封裝等org.springframework.core- 3.0.0.RELEASE-Spring3.0的核心工具包org.springframework.expression- 3.0.0.RELEASE-Spring表達(dá)式語言org.springframework.instrument.tomcat- 3.0.0.RELEASE-Spring3.0對Tomcat的連接池的集成org.springframework.instrument- 3.0.0.RELEASE-Spring3.0對服務(wù)器的代理
4、接口org.springframework.jdbc- 3.0.0.RELEASE-對JDBC的簡單封裝org.springframework.jms- 3.0.0.RELEASE-為簡化JMS API的使用而作的簡單封裝org.springframework.orm- 3.0.0.RELEASE-整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA實現(xiàn)org.springframework.oxm-3.0.0.RELEASE-Spring 對Object/XMl的映射支持,可以讓Java與XML之間來回切換org.springframework.tes
5、t- 3.0.0.RELEASE-對Junit等測試框架的簡單封裝org.springframework.transaction- 3.0.0.RELEASE-為JDBC、Hibernate、JDO、JPA等提供的一致的聲明式和編程式事務(wù)管理org.springframework.web.portlet- 3.0.0.RELEASE-SpringMVC的增強org.springframework.web.servlet- 3.0.0.RELEASE-對JEE6.0 Servlet3.0的支持org.springframework.web.struts- 3.0.0.RELEASE-整合Stru
6、ts的時候的支持org.springframework.web- 3.0.0.RELEASE-SpringWeb下的工具包 說明:jar包庫使用官方提供的,無需全部加載到項目中。 、spring對jdk的依賴: · JDK 6+ for Spring Framework 4.x· JDK 5+ for Spring Framework 3.x 、對spring的maven依賴以及介紹,請看官方簡介; 、spring各個版本的jar包全量下載地址,請點擊這里,有驚喜! 、什么?! 有些人想只要
7、獲取spring的一個jar包,不想全部都下載,那就請點擊這里。 2、借鑒spring官網(wǎng)寫法,建立一個src-resources Source Folder,再新建目錄META-INF,存放springmvc-servlet.xml和jdbc-context.xml文件(事務(wù)和數(shù)據(jù)庫連接池的管理);以及perties和perties。 JDBC-context.xml文件:Xml代碼 1. <?xml version="1.0" encoding="UT
8、F-8"?> 2. <beans xmlns="/schema/beans" 3. xmlns:xsi="/2001/XMLSchema-instance" 4. xmlns:aop="http:/www.
9、/schema/aop" 5. xmlns:tx="/schema/tx" 6. xmlns:context="/schema/context" 7.
10、; xsi:schemaLocation=" 8. /schema/beans 9. http:/www.springframework.or
11、g/schema/beans/spring-beans-3.0.xsd 10. /schema/tx 11. /schema/t
12、x/spring-tx-3.0.xsd 12. /schema/context 13. /schema/context/spring-con
13、text-3.0.xsd 14. /schema/aop 15. /schema/aop/spring-aop-3.0.xsd&
14、quot; default-autowire="byName"> 16. 17. <context:property-placeholder location="classpath:perties"/> 18. 19. <
15、;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 20. <property name="driverClass" value="$mysql.driverclass">&
16、lt;/property> 21. <property name="jdbcUrl" value="$mysql.jdbcurl"></property> 22. <property name="user" value=&quo
17、t;$mysql.user"></property> 23. <property name="password" value="$mysql.password"></property> 24. <property name="
18、acquireIncrement" value="5"></property> <!- 當(dāng)連接池中的連接用完時,C3P0一次性創(chuàng)建新連接的數(shù)目2 -> 25. <property name="initialPoolSize" value="10"></property>
19、160;<!- 初始化時創(chuàng)建的連接數(shù),必須在minPoolSize和maxPoolSize之間 -> 26. <property name="minPoolSize" value="20"></property> 27. <property&
20、#160;name="maxPoolSize" value="20"></property> 28. <!- 最大空閑時間,超過空閑時間的連接將被丟棄 29. 需要注意:mysql默認(rèn)的連接時長為8小時(28800)【可在my.ini中添加 wait_timeout=3
21、0(單位秒)設(shè)置連接超時】,這里設(shè)置c3p0的超時必須<28800 30. -> 31. <property name="maxIdleTime" value="300"></property> 32.
22、60; <property name="idleConnectionTestPeriod" value="60"></property> <!- 每60秒檢查連接池中的空閑連接 -> 33. <property name="maxStatement
23、s" value="20"></property> <!- jdbc的標(biāo)準(zhǔn)參數(shù) 用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatement數(shù)量,但由于預(yù)緩存的Statement屬 于單個Connection而不是整個連接 -> 34. </bean> 35.
24、;36. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37. <property name="dataSource" ref="dataSource
25、"></property> 38. </bean> 39. 40. <!- 聲明式事務(wù)管理 -> 41. <aop:config> 42.
26、 <aop:advisor pointcut="execution(* com.aokunsang.service.impl.*ServiceImpl.*(.)" advice-ref="myAdvice"/> 43. </aop:config> 44. <tx:a
27、dvice id="myAdvice" transaction-manager="txManager"> 45. <tx:attributes> 46. <tx:method name="add*"
28、160;propagation="REQUIRED"/> 47. <tx:method name="update*" propagation="REQUIRED"/> 48.
29、60;<tx:method name="*" read-only="true" rollback-for="com.aokunsang.util.DaoException"/> 49. </tx:attributes> 50. </tx:advice>
30、;51. 52. <!- 自動掃描組件,需要把controller去掉,否則影響事務(wù)管理 -> 53. <context:component-scan base-package="com.aokunsang"> 54.
31、; <context:exclude-filter type="regex" expression="com.aokunsang.web.*"/> 55. </context:component-scan> 56. </beans> springmvc-servlet.xml文件: Xml代碼
32、160;1. <?xml version="1.0" encoding="UTF-8"?> 2. <beans xmlns="/schema/beans" 3. xmlns:xsi="/2001/XMLSchema-instance" xmlns:p=&q
33、uot;/schema/p" 4. xmlns:context="/schema/context" 5. xmlns:mvc="/schema/mvc"
34、 6. xsi:schemaLocation=" 7. /schema/beans 8. ht
35、tp://schema/beans/spring-beans-3.0.xsd 9. /schema/context 10.
36、160;/schema/context/spring-context-3.0.xsd 11. /schema/mvc 12. &
37、#160;/schema/mvc/spring-mvc-3.0.xsd"> 13. 14. <!- 啟動掃描所有的controller -> 15. <context:component-scan base-package="com.aokunsang.w
38、eb"/> 16. 17. <!- 主要作用于Controller,激活該模式 18. 19. 下面是一種簡寫形式,完全可以手動配置替代這種簡寫形式; 2
39、0. 它會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean, 21. 是spring MVC為Controllers分發(fā)請求所必須的 22.
40、0;-> 23. <mvc:annotation-driven/> 24. 25. <!- 這里攔截器還有一種配置方法【針對路徑進(jìn)行配置】 推薦使用這個,方便直觀-> 26. <mvc:interceptors> 27.
41、 <mvc:interceptor> 28. <mvc:mapping path="/user/MyHome"/> 29. <
42、mvc:mapping path="/usermanager/*"/> 30. <bean class="erceptor.MyInterceptor"></bean> 31.
43、60;</mvc:interceptor> 32. </mvc:interceptors> 33. 34. <!- 全局配置 35. <mvc:interceptors> 36.
44、160; <bean class="erceptor.MyInterceptor"></bean> 37. </mvc:interceptors> 38. -> 39. <!- 配置js,css等靜態(tài)文件直接映射到
45、對應(yīng)的文件夾,不被DispatcherServlet處理 40. mapping - WebRoot里面的靜態(tài)文件位置目錄 41. location - 訪問地址 42. -> 43. <mvc:resources location=
46、"/resources/*" mapping="/WEB-INF/resources/"/> 44. 45. <!- 46. 配置/WEB-INF/views/目錄里面的jsp文件不被DispatcherServlet處理,可以直接通過瀏覽器訪問。 47.
47、; view-name - /WEB-INF/views/里面的jsp文件名(不許后綴即可) 48. path - 訪問地址 49. -> 50. <mvc:view-controller path="/header"
48、0;view-name="header"/> 51. 52. <!- jsp頁面解析器,當(dāng)Controller返回XXX字符串時,先通過攔截器,然后該類就會在/WEB-INF/views/目錄下,查找XXX.jsp文件-> 53. <bean class="org.springframework.web.servlet.view.InternalResourc
49、eViewResolver"> 54. <property name="prefix" value="/WEB-INF/views/"></property> 55. <property name="suffix" v
50、alue=".jsp"></property> 56. </bean> 57. </beans> 3、修改web.xml文件如下: Xml代碼 1. <!- Log4j的配置(在同一容器中部署多個應(yīng)用時, 2. 不能使用默認(rèn)的webAppRootKey,必須指定唯一KEY,以免沖突)
51、; 3. -> 4. <context-param> 5. <param-name>webAppRootKey</param-name> 6. <param-value>springmvc.root</param-value> 7. </context-param> 8
52、. 9. <context-param> 10. <param-name>contextConfigLocation</param-name> 11. <param-value>classpath:/META-INF/jdbc-context.xml</param-value> 12. </context-param>
53、; 13. 14. <listener> 15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16. </listener> 17. 18.
54、 <servlet> 19. <servlet-name>spring-mvc</servlet-name> 20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21. <init-p
55、aram> 22. <param-name>contextConfigLocation</param-name> 23. <param-value>classpath:/META-INF/springmvc-servlet.xml</param-value> 24.
56、0; </init-param> 25. <load-on-startup>1</load-on-startup> 26. </servlet> 27. 28. <servlet-mapping> 29. <servlet-name
57、>spring-mvc</servlet-name> 30. <url-pattern>/</url-pattern> 31. </servlet-mapping> 32. 33. <filter> 34. <filter-name>enco
58、dingFilter</filter-name> 35. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 36. </filter> 37. 38. <filter-mapping> 39
59、. <filter-name>encodingFilter</filter-name> 40. <url-pattern>/*</url-pattern> 41. </filter-mapping> 4、從springmvc-servlet.xml中可以知道,我把jsp頁面放在WEB-INF/views目錄中,靜態(tài)文件(圖片,j
60、s,css等)放在Resources目錄中,便于管理。 5、以上配置文件基本完成,下面開始代碼編寫:首先說幾個常用的注解: Java代碼 1. Autowired和Qualifier 自動注入根據(jù)類型注入 2. Autowired 可以對成員變量、方法以及構(gòu)造函數(shù)進(jìn)行注釋, 3. Qualifier 的標(biāo)注對象是成員變量、方法入?yún)?、?gòu)造函數(shù)入?yún)ⅰ?#160; 4. ps:兩者結(jié)合使用相當(dāng)
61、于Resourcede效果。 5. Resource 自動注入根據(jù)名稱注入,可寫參數(shù)name="" 6. Controller 表示控制器 7. Service 表示業(yè)務(wù)處理層一般在serviceImpl 8. Repository 表示持久層一般在daoImpl 9. Component 當(dāng)你的類不清楚是哪一層的時候使用該注解
62、60;10. ResponseBody 異步返回數(shù)據(jù)類型為json 11. RequestMapping 路徑,請求類型等設(shè)置 12. InitBinder 數(shù)據(jù)綁定 13. RequestBody、ModeleAttributes、SessionAttributes等 注解的詳細(xì)介紹: 也可以參考: <a>首先寫一個BaseController,可做一些數(shù)據(jù)綁定之類的全局操
63、作(如:把日期字符串轉(zhuǎn)換為Date日期)。 Java代碼 1. Controller 2. public class BaseController 3. 4. InitBinder 5. protected void ininBinder(WebDataBinder binder) 6.
64、0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 7. binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf,true); 8.
65、60; 9. <b>然后寫一個攔截器,攔截器有兩種實現(xiàn)方式,1、繼承HandlerInterceptorAdapter類,2、實現(xiàn)HandlerInterceptor接口。 Java代碼 1. /* 2. * 自定義攔截器 3. * author tushen 4. * date Nov 5, 2011 5. */
66、 6. public class MyInterceptor extends HandlerInterceptorAdapter 7. 8. /* 9. * 最后執(zhí)行,可用于釋放資源 10. */ 11. Ove
67、rride 12. public void afterCompletion(HttpServletRequest request, 13. HttpServletResponse response, Object handler, Exception ex) 1
68、4. throws Exception 15. / TODO Auto-generated method stub 16. super.afterComple
69、tion(request, response, handler, ex); 17. 18. 19. /* 20. * 顯示視圖前執(zhí)行 21. */ 22. Override
70、 23. public void postHandle(HttpServletRequest request, 24. HttpServletResponse response, Object handler, 25. &
71、#160; ModelAndView modelAndView) throws Exception 26. 27. System.out.println(request.getContentType()+"-"+request.getChar
72、acterEncoding()+"-"+request.getContextPath(); 28. System.out.println("MyInterceptor.postHandle()-viewName:"+modelAndView.getViewName(); 29. super.postHandle(reque
73、st, response, handler, modelAndView); 30. 31. 32. /* 33. * Controller之前執(zhí)行 34. */ 35. Overrid
74、e 36. public boolean preHandle(HttpServletRequest request, 37. HttpServletResponse response, Object handler) throws Exception
75、;38. 39. String url = request.getRequestURI(); 40. 41. Sys
76、tem.out.println("MyInterceptor.preHandle()"+url); 42. 43. return super.preHandle(request, response, handler); 44. &
77、#160;45. <c>在Util包中DBUtil.java中實現(xiàn)Spring JDBC Template的封裝,操作數(shù)據(jù)庫;寫一個DaoException繼承spring的運行時異常類NestedRuntimeException,在數(shù)據(jù)庫操作異常時拋出該異常,在controller層進(jìn)行處理。<d>寫一個抽象的BaseDao接口和BaseDaoImpl實現(xiàn)類,讓所有模塊共享使用(詳見附件)。 Java代碼 1. /* 2. * 3. */
78、0; 4. package com.aokunsang.dao; 5. 6. import java.io.Serializable; 7. import java.util.List; 8. import java.util.Map; 9. 10. /* 11. * author tushen 12. * date Nov
79、160;5, 2011 13. */ 14. public interface BaseDao 15. 16. /* 17. * 保存或者更新實體 18. * param sql 19.
80、160;* param entry 20. */ 21. <T extends Serializable> void saveOrUpdateObject(String sql,T entry); 22. 23. /*
81、0;24. * 查詢實體列表 25. * param sql 26. * param className 27. * param obj 28. * return 29. */ 30.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 關(guān)于加強中小學(xué)校固定資產(chǎn)管理的思考
- 信息化環(huán)境下企業(yè)財會工作職能轉(zhuǎn)變的研究
- 家具設(shè)計課件教學(xué)課件
- 53模擬試卷初中數(shù)學(xué)八年級下冊16.1.1二次根式的概念
- DB14-T 2987-2024 山西電子政務(wù)外網(wǎng)電子認(rèn)證系統(tǒng)總體架構(gòu)
- 保健食品行業(yè)供需現(xiàn)狀與發(fā)展戰(zhàn)略規(guī)劃
- 中國金屬導(dǎo)體漿料市場發(fā)展?fàn)顩r與投資規(guī)劃建議報告2024-2030年
- 睿創(chuàng)微納財務(wù)報表分析報告
- 八年級物理第一次月考卷01(參考答案)(安徽滬科版)
- 河北省正定縣七中2025年高三第一次測試語文試題含解析
- 板球運動簡介課件
- 兵團(tuán)精神講座-課件
- 師德師風(fēng)量化考核表
- 小學(xué)硬筆書法課課件
- 學(xué)校中層干部培訓(xùn)課件
- 硬質(zhì)合金生產(chǎn)重點技術(shù)之壓制和燒結(jié)
- 小學(xué)特殊教育人教三年級上冊目錄教學(xué)設(shè)計《小臺燈》第一課時
- 門診診斷證明書(模板)
- 初中 初二 生物 鳥 任務(wù)單
- 2021年新教科版六年級上冊科學(xué)第四單元能量教材分析
- 分項工程質(zhì)量驗收記錄表格模板
評論
0/150
提交評論