1、新建web项目,将所需jar包放到 lib 目录下
2、配置web.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>SchoolResourceRepository</display-name> 4 <filter> 5 <filter-name>Set Character Encoding</filter-name> 6 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 <init-param> 8 <param-name>encoding</param-name> 9 <param-value>utf-8</param-value> 10 </init-param> 11 </filter> 12 <filter-mapping> 13 <filter-name>Set Character Encoding</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 <context-param> 17 <param-name>CharEncode</param-name> 18 <param-value>utf-8</param-value> 19 </context-param> 20 <!-- <filter> 21 <filter-name>charEncode</filter-name> 22 <filter-class>net.wwwyibu.filter.CharEncodeFilter</filter-class> 23 <async-supported>true</async-supported> 24 </filter> 25 <filter-mapping> 26 <filter-name>charEncode</filter-name> 27 <url-pattern>*.do</url-pattern> 28 </filter-mapping> 29 <filter-mapping> 30 <filter-name>charEncode</filter-name> 31 <url-pattern>*.jsp</url-pattern> 32 </filter-mapping> 33 <filter-mapping> 34 <filter-name>charEncode</filter-name> 35 <url-pattern>*.action</url-pattern> 36 </filter-mapping> 37 <filter-mapping> 38 <filter-name>charEncode</filter-name> 39 <url-pattern>/*</url-pattern> 40 </filter-mapping>--> 41 42 <!-- <filter> 43 <filter-name>loginFilter</filter-name> 44 <filter-class>net.wwwyibu.filter.StuZHMassgeLoginFilter</filter-class> 45 </filter> 46 <filter-mapping> 47 <filter-name>loginFilter</filter-name> 48 <url-pattern>/stuZHMessage/*</url-pattern> 49 </filter-mapping> --> 50 51 52 <!-- <filter> 53 <filter-name>loginFilter</filter-name> 54 <filter-class>net.wwwyibu.filter.LoginOutFilter</filter-class> 55 </filter> 56 <filter-mapping> 57 <filter-name>loginFilter</filter-name> 58 <url-pattern>/*</url-pattern> 59 </filter-mapping> --> 60 61 <context-param> 62 <param-name>contextConfigLocation</param-name> 63 <param-value> 64 WEB-INFclassesapplicationContext.xml</param-value> 65 </context-param> 66 <listener> 67 <listener-class> 68 org.springframework.web.context.ContextLoaderListener 69 </listener-class> 70 </listener> 71 <!-- 监听初始化学校代码 --> 72 <!-- <listener> 73 <listener-class>net.wwwyibu.filter.ServletListener</listener-class> 74 </listener> --> 75 76 77 <servlet-mapping> 78 <servlet-name>default</servlet-name> 79 <url-pattern>/static/*</url-pattern> 80 </servlet-mapping> 81 <servlet> 82 <servlet-name>springmvc</servlet-name> 83 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 84 <init-param> 85 <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description> 86 <param-name>contextConfigLocation</param-name> 87 <param-value>WEB-INFclassesapplicationContext.xml</param-value> 88 </init-param> 89 <load-on-startup>1</load-on-startup> 90 </servlet> 91 <servlet-mapping> 92 <servlet-name>springmvc</servlet-name> 93 <url-pattern>/</url-pattern> 94 </servlet-mapping> 95 <filter> 96 <filter-name>HiddenHttpMethodFilter</filter-name> 97 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 98 </filter> 99 <filter-mapping> 100 <filter-name>HiddenHttpMethodFilter</filter-name> 101 <servlet-name>springmvc</servlet-name> 102 </filter-mapping> 103 <welcome-file-list> 104 <welcome-file>index.html</welcome-file> 105 <welcome-file>index.htm</welcome-file> 106 <welcome-file>index.jsp</welcome-file> 107 <welcome-file>default.html</welcome-file> 108 <welcome-file>default.htm</welcome-file> 109 <welcome-file>default.jsp</welcome-file> 110 </welcome-file-list> 111 </web-app>
3、SpringContext.xml配置文件 和 Spring-mvc.xml配置文件,这两个可以写到一个xml文件中,applicationContext.xml中。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/util spring-util-3.2.xsd 8 http://www.springframework.org/schema/tx spring-tx-3.2.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 10 http://www.springframework.org/schema/aop spring-aop-3.2.xsd 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 12 <!-- 让Spring通过自动扫描来查询和管理Bean --> 13 <context:component-scan base-package="net.wwwjpz" /> 14 15 <context:annotation-config /> 16 17 <mvc:annotation-driven /> 18 <!-- 加載 properties 配置文件 --> 19 <bean id="propertyConfig" 20 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 21 <property name="location"> 22 <value>classpath:jdbc.properties</value> 23 <!-- <value>file:D:/YiBuProperties/SchoolResourceRepository/jdbc.properties</value> --> 24 </property> 25 <property name="ignoreUnresolvablePlaceholders" value="true" /> 26 </bean> 27 28 <!-- <bean id="ExcelDatabase" class="net.wwwyibu.service.ExcelDatabase"></bean> 29 <bean id="MedicalService" class="net.wwwyibu.service.MedicalService"></bean> 30 <bean id="StudentService" class="net.wwwyibu.service.StudentService"></bean> 31 <bean id="UploadService" class="net.wwwyibu.service.UploadService"></bean> 32 <bean id="SaveHealthService" class="net.wwwyibu.service.SaveHealthService"></bean> 33 <bean id="EvaluationService" class="net.wwwyibu.service.EvaluationService"></bean> 34 <bean id="ZhsjService" class="net.wwwyibu.service.ZhsjService"></bean> 35 <bean id="ExamService" class="net.wwwyibu.service.ExamService"></bean> --> 36 <!-- <bean id="ExcelCheckService" class="net.wwwyibu.service.ExcelCheckService"></bean> --> 37 38 39 40 41 <!-- 42 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 43 <property name="defaultEncoding" value="UTF-8"/> 44 <property name="maxUploadSize" value="20971520"/> 45 <property name="resolveLazily" value="true"/> 46 </bean> --> 47 48 <bean id="dateSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> 49 <property name="driver"> 50 <value>${jdbc.driver}</value> 51 </property> 52 <property name="driverUrl"> 53 <value>${jdbc.url}</value> 54 </property> 55 <property name="user"> 56 <value>${jdbc.username}</value> 57 </property> 58 <property name="alias"> 59 <value>${jdbc.username}</value> 60 </property> 61 <property name="password"> 62 <value>${jdbc.password}</value> 63 </property> 64 65 <property name="maximumActiveTime"> 66 <value>1200000</value> 67 </property> 68 <property name="prototypeCount"> 69 <value>100</value> 70 </property> 71 <property name="maximumConnectionCount"> 72 <value>100</value> 73 </property> 74 <property name="minimumConnectionCount"> 75 <value>10</value> 76 </property> 77 <property name="simultaneousBuildThrottle"> 78 <value>500</value> 79 </property> 80 <property name="trace"> 81 <value>true</value> 82 </property> 83 <property name="verbose"> 84 <value>true</value> 85 </property> 86 <property name="testBeforeUse"> 87 <value>true</value> 88 </property> 89 <property name="houseKeepingTestSql"> 90 <value>select 1 from dual</value> 91 </property> 92 93 </bean> 94 95 96 <bean id="transactionManager" 97 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 98 <property name="dataSource"> 99 <ref bean="dateSource" /> 100 </property> 101 </bean> 102 103 <bean id="transactionManagerHib" 104 class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 105 <property name="sessionFactory"> 106 <ref local="sessionFactory" /> 107 </property> 108 </bean> 109 110 111 <bean id="transactionInterceptor" 112 class="org.springframework.transaction.interceptor.TransactionInterceptor"> 113 <property name="transactionManager" ref="transactionManagerHib" /> 114 115 <property name="transactionAttributes"> 116 <props> 117 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 118 <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> 119 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> 120 <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> 121 <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop> 122 <prop key="insert*">PROPAGATION_REQUIRED</prop> 123 <prop key="update*">PROPAGATION_REQUIRED</prop> 124 <prop key="save*">PROPAGATION_REQUIRED</prop> 125 <prop key="delete*">PROPAGATION_REQUIRED,-BussException</prop> 126 <prop key="*">PROPAGATION_REQUIRED</prop> 127 </props> 128 </property> 129 </bean> 130 131 132 133 134 <bean id="autoProxyCreator" 135 class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 136 <property name="interceptorNames"> 137 <list> 138 <idref bean="transactionInterceptor" /> 139 </list> 140 </property> 141 <property name="beanNames"> 142 <list> 143 <value>/yeepayCallBack,Add_Re,*DAO</value> 144 </list> 145 </property> 146 </bean> 147 148 <bean id="hibernateDaoTemplate" abstract="true"> 149 <property name="sessionFactory"> 150 <ref bean="sessionFactory" /> 151 </property> 152 </bean> 153 154 155 <bean id="sessionFactory" 156 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 157 <property name="dataSource"> 158 <ref bean="dateSource" /> 159 </property> 160 <property name="hibernateProperties"> 161 <props> 162 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 163 <!-- <prop key="hibernate.dialect">net.wwwyibu.util.MySQL5DialectRegexp</prop> --> 164 <prop key="hibernate.show_sql">true</prop> 165 <prop key="hibernate.hbm2ddl.auto">none</prop> 166 167 <!--<prop key="hibernate.current_session_context_class">thread</prop> --> 168 <prop key="hibernate.jdbc.batch_size">50</prop> 169 <prop key="hibernate.jdbc.fetch_size">50</prop> 170 <!--代表使用Hibernate的二级缓存 --> 171 <prop key="hibernate.cache.use_second_level_cache">true</prop> 172 <!--代表是否使用查询缓存,这里不使用,因为一般而言查询缓存的命中率并不是很高,所以我们没有 必要为每一个用户的查询缓存它的数据,所以这里设为false --> 173 <prop key="hibernate.cache.use_query_cache">false</prop> 174 <!--用于指定使用缓存产品的驱动类 --> 175 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 176 </props> 177 </property> 178 <property name="mappingResources"> 179 <list> 180 <!-- <value>net/wwwyibu/orm/Student.hbm.xml</value> 181 <value>net/wwwyibu/orm/Studentclass.hbm.xml</value> --> 182 </list> 183 </property> 184 </bean> 185 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 186 <property name="sessionFactory"> 187 <ref bean="sessionFactory" /> 188 </property> 189 </bean> 190 <bean id="multipartResolver" 191 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 192 <!-- 上传文件大小上限,单位为字节(10MB) --> 193 <property name="maxUploadSize"> 194 <value>10485760</value> 195 </property> 196 <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 --> 197 <property name="defaultEncoding"> 198 <value>UTF-8</value> 199 </property> 200 </bean> 201 202 <!-- mvc --> 203 204 <bean 205 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 206 <bean 207 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 208 209 <!-- Default ViewResolver --> 210 <bean id="viewResolver" 211 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 212 <property name="viewClass" 213 value="org.springframework.web.servlet.view.JstlView" /> 214 <property name="prefix" value="/" /> 215 <property name="suffix" value=".jsp"></property> 216 </bean> 217 <mvc:default-servlet-handler /> 218 <!-- mvc --> 219 220 221 </beans>