以下是一個最簡單的示例
1、新建一個標準的javaweb項目
2、導入spring所需的一些基本的jar包
3、配置web.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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"> <!-- 應用程序spring上下文配置 --> <context-param> <param-name>contextconfiglocation</param-name> <param-value> classpath*:applicationcontext*.xml, </param-value> </context-param> <!-- spring上下文加載監聽器 --> <listener> <listener- class > org.springframework.web.context.contextloaderlistener </listener- class > </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
4、添加spring配置文件applicationcontext
5、對applicationcontext.xml文件做最簡單的配置
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default -lazy-init= "false" default -autowire= "byname" > <bean id= "user" class = "com.po.user" > <property name= "name" value= "張三" /> </bean> </beans> |
beans——xml文件的根節點。
xmlns——是xmlnamespace的縮寫,因為xml文件的標簽名稱都是自定義的,自己寫的和其他人定義的標簽很有可能會重復命名,而功能卻不一樣,所以需要加上一個namespace來區分這個xml文件和其他的xml文件,類似于java中的package。
xmlns:xsi——是指xml文件遵守xml規范,xsi全名:xmlschemainstance,是指具體用到的schema資源文件里定義的元素所準守的規范。即/spring-beans-2.0.xsd這個文件里定義的元素遵守什么標準。
xsi:schemalocation——是指,本文檔里的xml元素所遵守的規范,schemalocation屬性用來引用(schema)模式文檔,解析器可以在需要的情況下使用這個文檔對xml實例文檔進行校驗。它的值(uri)是成對出現的,第一個值表示命名空間,第二個值則表示描述該命名空間的模式文檔的具體位置,兩個值之間以空格分隔。
6、新建一個實體類user.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.po; public class user { private string name; private string age; public string getname() { return name; } public void setname(string name) { this .name = name; } public string getage() { return age; } public void setage(string age) { this .age = age; } } |
7、測試
1
2
3
4
5
6
|
public static void main(string[] args) { // todo auto-generated method stub applicationcontext ac = new filesystemxmlapplicationcontext( "config/applicationcontext.xml" ); user user =(user)ac.getbean( "user" ); system.out.println(user.getname()); } |
輸出
這就實現web項目搭建基礎spring框架。接下來就做一些真正項目中會用到的一些擴展
可以在web.xml中配置一些spring框架集成的功能或其他設置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<!-- 字符編碼過濾器,必須放在過濾器的最上面 --> <filter> <filter-name>encodingfilter</filter-name> <filter- class >org.springframework.web.filter.characterencodingfilter</filter- class > <init-param> <param-name>forceencoding</param-name> <param-value> true </param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>utf- 8 </param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置延遲加載時使用opensessioninview--> <filter> <filter-name>opensessioninviewfilter</filter-name> <filter- class > org.springframework.orm.hibernate3.support.opensessioninviewfilter </filter- class > <init-param> <param-name>singlesession</param-name> <param-value> true </param-value> </init-param> <init-param> <param-name>sessionfactorybeanname</param-name> <!--指定對spring配置中哪個sessionfactory使用opensessioninview--> <param-value>sessionfactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>opensessioninviewfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring security過濾器 org.springframework.web.filter.delegatingfilterproxy(委托過濾器代理)--> <!-- 使用springsecurity或apache shiro就會用到這個過濾器,--> <filter> <filter-name>springsecurityfilterchain</filter-name> <filter- class >org.springframework.web.filter.delegatingfilterproxy</filter- class > </filter> <filter-mapping> <filter-name>springsecurityfilterchain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 聲明 spring mvc dispatcherservlet --> <servlet> <servlet-name>springdispatcher</servlet-name> <servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class > <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <!-- map all requests for /* to the dispatcher servlet --> <servlet-mapping> <servlet-name>springdispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置出錯頁面 --> <error-page> <error-code> 404 </error-code> <location>errorpage/ 404 .jsp</location> </error-page> <!-- 401 錯誤 --> <error-page> <error-code> 401 </error-code> <location>/errorpage/ 401 .html</location> </error-page> <!-- 為每個jsp頁面引入taglib.jspf等文件 --> <jsp-config> <taglib> <taglib-uri>/web-inf/runqianreport4.tld</taglib-uri> <taglib-location>/web-inf/runqianreport4.tld</taglib-location> </taglib> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>utf- 8 </page-encoding> <include-prelude>/tag/taglib.jspf</include-prelude> <!--<trim-directive-whitespaces> true </trim-directive-whitespaces> --> </jsp-property-group> </jsp-config> |
其中jspf就是做一些全局的聲明
1
2
3
4
5
6
7
8
9
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" <span style= "white-space:pre" > </span>pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix= "fn" uri= "http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix= "fmt" uri= "http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix= "fnc" uri= "/web-inf/tlds/fnc.tld" %> <%@ taglib tagdir= "/web-inf/tags" prefix= "mytag" %> <c:set var= "ctx" scope= "session" <span style= "white-space:pre" > </span>value= "${pagecontext.request.contextpath}" /> |
可以在applicationcontext.xml中配置更多的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
<!-- beans可以添加更多聲明 --> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:jee= "http://www.springframework.org/schema/jee" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:aop= "http://www.springframework.org/schema/aop" 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/jee http://www.springframework.org/schema/jee/spring-jee.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default -lazy-init= "false" default -autowire= "byname" > <!-- 使用注解定義切面 --> <aop:aspectj-autoproxy /> <mvc:annotation-driven /> <!-- spring 注釋代替配置,自動掃描的基礎包,將掃描該包以及所有子包下的所有類需要把controller去掉,否則影響事務管理 --> <context:component-scan base- package = "com.schoolnet" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.controller" /> </context:component-scan> <!-- 配置系統properties配置文件 --> <bean id= "propertyconfigurer" class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" > <property name= "fileencoding" value= "utf-8" /> <property name= "locations" > <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 數據源配置 --> <bean id= "datasource" class = "com.mchange.v2.c3p0.combopooleddatasource" destroy-method= "close" > <property name= "driverclass" value= "${jdbc.driverclassname}" /> <property name= "jdbcurl" value= "${jdbc.url}" /> <property name= "user" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> <property name= "minpoolsize" > <value> 1 </value> </property> <property name= "maxpoolsize" value= "100" /> <property name= "initialpoolsize" value= "3" /> <!--最大空閑時間, 60 秒內未使用則連接被丟棄。若為 0 則永不丟棄。 default : 0 --> <property name= "maxidletime" value= "60" /> <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。 default : 3 --> <property name= "acquireincrement" value= "5" /> <property name= "maxstatements" value= "0" /> <!--每 60 秒檢查所有連接池中的空閑連接。 default : 0 --> <property name= "idleconnectiontestperiod" value= "60" /> <!--定義在從數據庫獲取新連接失敗后重復嘗試的次數。 default : 30 --> <property name= "acquireretryattempts" value= "30" /> <!-- 獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數據源仍有效 保留,并在下次調用getconnection()的時候繼續嘗試獲取連接。如果設為 true ,那么在嘗試 獲取連接失敗后該數據源將申明已斷開并永久關閉。 default : false --> <property name= "breakafteracquirefailure" value= "false" /> <!-- 因性能消耗大請只在需要的時候使用它。如果設為 true 那么在每個connection提交的 時候都將校驗其有效性。建議使用idleconnectiontestperiod或automatictesttable 等方法來提升連接測試的性能。 default : false --> <property name= "testconnectiononcheckout" value= "false" /> </bean> <!-- 定義事務管理器(聲明式的事務)--> <!-- 支持 @transactional 標記 --> <!-- 方式一:datasourcetransactionmanager --> <bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" > <property name= "datasource" ref= "datasource" /> </bean> <tx:annotation-driven transaction-manager= "transactionmanager" /> <!-- 方式二:hibernatetransactionmanager --> <bean id= "hibernatetransactionmanager" class = "org.springframework.orm.hibernate3.hibernatetransactionmanager" > <property name= "sessionfactory" > <ref local= "sessionfactory" /> </property> </bean> <!-- 配置hibernate的session工廠 --> <bean id= "sessionfactory" class = "org.springframework.orm.hibernate3.localsessionfactorybean" > <property name= "datasource" ref= "datasource" /> <property name= "lobhandler" ref= "lobhandler" /> <property name= "mappinglocations" > <list> <value>classpath*:/com/schoolnet /**/ *.hbm.xml</value> </list> </property> <property name= "hibernateproperties" > <props> <!-- 解決在oracle多個表空間表名相同導致hibernate不會自動生成表 --> <prop key= "hibernate.default_schema" >${jdbc.username}</prop> <prop key= "hibernate.dialect" > org.hibernate.dialect.oracle10gdialect </prop> <prop key= "hibernate.show_sql" > true </prop> <!--解決內存泄漏問題 --> <prop key= "hibernate.generate_statistics" > false </prop> <prop key= "hibernate.connection.release_mode" > auto </prop> <prop key= "hibernate.autoreconnect" > true </prop> <prop key= "hibernate.cache.provider_class" > org.hibernate.cache.ehcacheprovider </prop> <!--解決內存泄漏問題 --> <prop key= "hibernate.cache.use_query_cache" > false </prop> <prop key= "use_second_level_cache" > false </prop> <prop key= "hibernate.hbm2ddl.auto" >update</prop> <prop key= "current_session_context_class" >thread</prop> </props> </property> <property name= "eventlisteners" > <map> <entry key= "merge" > <bean class = "org.springframework.orm.hibernate3.support.idtransferringmergeeventlistener" /> </entry> </map> </property> </bean> <!-- 2 .配置hibernate事務特性 --> <tx:advice id= "txadvice" transaction-manager= "hibernatetransactionmanager" > <tx:attributes> <tx:method name= "save*" propagation= "required" rollback- for = "exception" /> <tx:method name= "add*" propagation= "required" rollback- for = "exception" /> <tx:method name= "update*" propagation= "required" rollback- for = "exception" /> <tx:method name= "modify*" propagation= "required" rollback- for = "exception" /> <tx:method name= "del*" propagation= "required" rollback- for = "exception" /> <tx:method name= "start*" propagation= "required" rollback- for = "exception" /> <tx:method name= "stop*" propagation= "required" rollback- for = "exception" /> <tx:method name= "assign*" propagation= "required" rollback- for = "exception" /> <tx:method name= "clear*" propagation= "required" rollback- for = "exception" /> <tx:method name= "execute*" propagation= "required" rollback- for = "exception" /> <tx:method name= "insert*" propagation= "required" rollback- for = "exception" /> <tx:method name= "do*" propagation= "required" rollback- for = "exception" /> <tx:method name= "set*" propagation= "required" rollback- for = "exception" /> <tx:method name= "*n" propagation= "never" /> <tx:method name= "*" read-only= "true" /> </tx:attributes> </tx:advice> <!-- 配置那些類的方法進行事務管理 --> <aop:config> <aop:advisor pointcut= "execution(* com.eshine..*.service.*.*(..))" advice-ref= "txadvice" /> </aop:config> |
spring-mvc.xml文件配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?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:jee= "http://www.springframework.org/schema/jee" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:aop= "http://www.springframework.org/schema/aop" 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.xsd http: //www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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"> <context:component-scan base- package = "com.schoolnet" use- default -filters= "false" > <context:include-filter type= "annotation" expression= "org.springframework.stereotype.controller" /> </context:component-scan> <mvc:annotation-driven /> <mvc: default -servlet-handler /> <!-- jsp視圖解析器 --> <bean id= "jspviewresolver" class = "org.springframework.web.servlet.view.internalresourceviewresolver" > <property name= "prefix" value= "/" /> <property name= "suffix" value= ".jsp" /> <property name= "order" value= "0" /> <property name= "contenttype" value= "text/html;charset=utf-8" /> </bean> <!--多文上傳,限制1g文件 --> <bean id= "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver" > <property name= "maxuploadsize" value= "1073741824" /> </bean> </beans> |
總結
以上就是本文關于spring框架web項目實戰全代碼分享的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/zz_cl/article/details/52502168