配置文件 web.xml + applicationContext.xml + Spring-servlet.xml
<?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: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-4.2.xsd http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc?http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!--?配置自動(dòng)掃描的包?--> <context:component-scan?base-package="com.imooc"> <context:include-filter?type="annotation" expression="org.springframework.stereotype.Controller"?/> <context:exclude-filter?type="annotation" expression="org.springframework.stereotype.Service"?/> <context:exclude-filter?type="annotation" expression="org.springframework.stereotype.Repository"?/> </context:component-scan> <mvc:resources?location="/images/"?mapping="/images/**"?/> <mvc:resources?location="/js/"?mapping="/js/**"?/> <mvc:resources?location="/style/"?mapping="/style/**"?/> <mvc:resources?location="/img/"?mapping="/img/**"?/> <bean class="org.springframework.web.servlet.mvc.methdo.annotation.RequestMappingHandlerMapping"></bean> <bean class="org.springframework.web.servlet.mvc.methdo.annotation.RequestMappingHandlerAdapter"> <property?name="cacheSeconds"?value="0"?/> <property?name="messageConverters"> <list> <ref?bean="mappingJacksonHttpMessageConverter"?/> <ref?bean="mappingStringHttpMessageConverter"?/> </list> </property> <property?name="webBindInitializer"?ref="webBindInitializer"></property> </bean> <bean?id="mappingStringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property?name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean?id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property?name="supportedMediaTypes"> <list> <bean?class="org.springframework.http.MediaType"> <constructor-arg?index="0"?value="application"></constructor-arg> <constructor-arg?index="1"?value="json"></constructor-arg> <constructor-arg?index="2"?value="UTF-8"></constructor-arg> </bean> </list> </property> </bean> <bean?id=""?webBindInitializer?" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property?name="conversionService"> <bean class="org.springframework.core.convert.support.DefaultConversionService?/> </property> </bean> <!--?配置視圖解析器?如何把?handler?方法返回值解析為實(shí)際的物理視圖?--> <bean??id=" ViewResolver?" class="org.springframework.web.servlet.view.InternalResourceViewResolver?"> <property?name="ignoreAcceptHeader"?value="true"></property> <property?name="mediaTypes"> <map> <entry?key="json"?value="application/json"></entry> <entry?key="xml"?value="application/xml"></entry> </map> </property> <property?name="defaultContentType"?value="text/html"></property> <property?name="favorParameter"?value="false"></property> <property?name="ViewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property?name="order"?value="2"?/> <property?name="prefix"?value="/WEB-INF/views/"?/> <property?name="suffix"?value=".jsp"?/> </bean> </list> </property> </bean> </beans>
Spring-servlet.xml ?
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"?xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans? ???http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop? http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context ?http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx? http://www.springframework.org/schema/tx/spring-tx-4.3.xsd?"> <!--?引入外部的屬性文件?--> <context:property-placeholder?location="classpath:/jdbc.properties"?/> <aop:aspectj-autoproxy?/> <!--?baosaomiao?--> <context:component-scan?base-package="com"> <context:exclude-filter?type="annotation" expression="org.springframework.stereotype.Controller"?/> </context:component-scan> <!--?配置c3p0?連接池?--> <bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property?name="driverClass"?value="${jdbc.driverClass}"?/> <property?name="jdbcUrl"?value="${jdbc.url}"?/> <property?name="user"?value="${jdbc.username}"?/> <property?name="password"?value="${jdbc.password}"?/> </bean> <!--?配置hibernate?相關(guān)屬性?--> <bean?id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!--?注入連接池?--> <property?name="dataSource"?ref="dataSource"></property> <!--?hibernate?的相關(guān)屬性?--> <property?name="hibernateProperties"> <props> <prop?key="hibernate.show_sql">true</prop> <prop?key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop?key="hibernate.format_sql">true?</prop> <prop?key="hibernate.cache_use_second_level_cache">false</prop> <prop?key="hibernate.cache_use_query_cache">false</prop> <prop?key="current_session_context_class">thread</prop> <prop?key="hibernate.current_session_class">org.springframework.orm.hibernate4.SpringSessionContext </prop> <!--?它包含4個(gè)屬性:?*?create?:?會(huì)根據(jù)你的model類來(lái)生成表,但是每次運(yùn)行都會(huì)刪除上一次的表,重新生成表,哪怕2次沒(méi)有任何改變? *?create-drop?:?根據(jù)model類生成表,但是sessionFactory一關(guān)閉,表就自動(dòng)刪除?*?update?:?最常用的屬性,也根據(jù)model類生成表,即使表結(jié)構(gòu)改變了,表中的行仍然存在,不會(huì)刪除以前的行? *?validate?:?只會(huì)和數(shù)據(jù)庫(kù)中的表進(jìn)行比較,不會(huì)創(chuàng)建新表,但是會(huì)插入新值?--> <prop?key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!--?hibernate?映射文件?--> <property?name="packagesToScan"> <list> <value>com.imooc.ssh.entity</value> </list> </property> </bean> <!--?配置事務(wù)管理器?--> <bean?id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property?name="sessionFactory"?ref="sessionFactory"></property> </bean> <!--?開(kāi)啟事務(wù)注解?--> <!--?<tx:annotation-driven?transaction-manager="transactionManager"?/>?--> <!--?開(kāi)啟事務(wù)注解?--> <tx:advice?id="txAdvice"?transaction-manager="transactionManager"> <tx:attributes> <tx:method?name="get*"?read-only="true"?propagation="REQUIRED"?/> <tx:method?name="list*"?read-only="true"?propagation="REQUIRED"?/> <tx:method?name="find*"?read-only="true"?propagation="REQUIRED"?/> <tx:method?name="*"?read-only="false"?propagation="REQUIRED" rollback-for="Exception"?/> </tx:attributes> </tx:advice> <aop:config?proxy-target-class="true"> <aop:advisor?pointcut="execution(public?*?com.imooc.*.Service.*Service.*())" advice-ref="txAdvice"?/> </aop:config> </beans>
applicationContext.xml
<?xml?version="1.0"?encoding="UTF-8"?> <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"?version="3.0"> <display-name>SpringMVC_Spring_Hibernate</display-name> <!--?防止中文參數(shù)亂碼?放在前面?--> <filter> <filter-name>SetCharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> <!--?強(qiáng)制進(jìn)行轉(zhuǎn)碼?--> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--?spring?配置Listener?--> <!--?needed?for?ContextLoaderListener?--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--?springmvc配置dispatcherServlet?--> <!--?The?front?controller?of?this?Spring?Web?application,?responsible?for? handling?all?application?requests?--> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/resources/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--?Map?all?requests?to?the?DispatcherServlet?for?handling?--> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
web.xml
基于慕課網(wǎng)小伙伴 ?@須木一瓜3791985 ? 站在巨人的肩膀上 ? 謝謝這位小伙伴 ?但是 ?這個(gè)我沒(méi)有測(cè)試過(guò) ?但是應(yīng)該基本不會(huì)錯(cuò) ? 祝大家成功 ?不要輕易放棄 ? 如果需要jar包的加我微信 ?allpper 發(fā)你