springmvc 配置過程及詳解

加入jar包html

在web.xml中mysql

添加spring監聽器web

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

添加spring容器(父容器)配置文件:spring

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/WEB-INF/config/application-context.xml			<!--聲明數據庫鏈接參數和事務管理-->
		/WEB-INF/config/customer-admin-manage.xml			<!--dao和service-->
	</param-value>
</context-param>

spring配置文件application-context.xmlsql

數據庫配置文件 jdbc.properties:數據庫

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cms?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
...

添加到spring父容器的配置文件application-context.xml中api

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>/WEB-INF/config/jdbc.properties</value>
		</list>
	</property>
</bean>

配置dataSource:緩存

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<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="autoCommitOnClose" value="true"/>
	<property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
	<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
	<property name="minPoolSize" value="${cpool.minPoolSize}"/>
	<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
	<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
	<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
	<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>

配置sessionFactory:cookie

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>	
	<property name="mappingLocations">
		<list>
			<value>classpath*:/com/cms/customer/entity/hbm/*.hbm.xml</value>
		</list>
	</property>
	<property name="hibernateProperties">
		<value>
		hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
		hibernate.show_sql=false
		hibernate.format_sql=false
		hibernate.query.substitutions=true 1, false 0
		hibernate.jdbc.batch_size=20
		hibernate.cache.use_query_cache=true
		</value>
	</property>
	<property name="entityInterceptor">< !-- 配置Hibernate攔截器,自動填充數據的插入、更新時間(不知道什麼意思)-->
		<ref local="treeInterceptor"/>
	</property>
	<property name="cacheProvider">< !-- 爲WEB應用提供緩存。 -->
		<ref local="cacheProvider"/>
	</property>
	<property name="lobHandler">< !-- spring提供的操做lob字段。<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/> -->
		<ref bean="lobHandler" />
	</property>
</bean>

基於全註解的事務聲明管理session

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />

對於context:annotation-config 他的做用是隱式地向 Spring 容器註冊AutowiredAnnotationBeanPostProcessor(想使用Autowired註解)、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor這 4 個BeanPostProcessor。

另外,<context:component-scan base-package=」XX.XX」/>
(用於自動掃描須要注入的bean) 包含了<context:annotation-config/>
的功能,在這裏沒有使用<context:component-scan base-package=」XX.XX」/>,
則必須將bean添加到父容器的(dao,service,manager之類的)xml中

對於tx:annotation-driven  ,則表示全部Transactional註解了的manager都使用這個事務管理

配置springmvc(子容器)

添加springmvc的Servlet

<servlet>
	<servlet-name>admin</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<!--指定注入action的配置文件,若是沒有指定,則默認在web-inf下查找admin-servlet.xml	-->
		<param-value>/WEB-INF/config/admin.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

每個servlet對應一個配置文件,用於映射不一樣的請求路徑集合

<servlet>
	<servlet-name>front</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/front.xml</param-value>
	</init-param>
	<load-on-startup>2</load-on-startup>
</servlet>

對於admin.xml

<!--國際化聲明-->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<property name="cacheSeconds" value="-1"/>
	<property name="basenames">
		<list>
			<value>/WEB-INF/languages/core_admin/messages</value>
		</list>
	</property>
</bean>

<!--文件上傳-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

<!--經過註解,把一個URL映射到Controller類的方法上-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	<property name="webBindingInitializer"><!--重寫WebBindingInitializer-->
		<bean class=" com.cms.common.web.springmvc.BindingInitializer"/>
	</property>
</bean>

<!--用於Spring 從外部屬性文件中載入屬性,並使用這些屬性值替換Spring 配置文件中的佔位符變量(${varible})。 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>/WEB-INF/config/firewall.properties</value>
		</list>
	</property>
</bean>

<!--	DefaultAnnotationHandlerMapping-映射url到被RequestMapping註解的controller或者下面的方法-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
	<property name="interceptors">
		<list>
			<ref bean="adminContextInterceptor"/>
			<ref bean="adminLocaleIntercept"/>
			<ref bean="fireWallInterceptor"/>
		</list>
	</property>
</bean>
<!--攔截器-->
<bean id="adminContextInterceptor" class="com.cms.cms.web.AdminContextInterceptor">
	<property name="auth" value="true"/>
	<property name="loginUrl" value="/admin/cms/login.do"/>
	<property name="returnUrl" value="/admin/cms/index.do"/>
	<property name="excludeUrls">
		<list>
			<value>/login.do</value>
			<value>/logout.do</value>
		</list>
	</property>
</bean>
<bean id="adminLocaleIntercept" class="com.cms.cms.web.AdminLocaleInterceptor"/>
<bean id="fireWallInterceptor" class="com.cms.cms.web.FireWallInterceptor"></bean>

<!--Cookie相關-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
	<property name="cookieName" value="clientlanguage"/>
	<property name="cookieMaxAge" value="-1"/>
</bean>

<!--定義一場處理-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="exceptionMappings">
		<props>
			<prop key="org.springframework.web.bind.MissingServletRequestParameterException">/error/requiredParameter</prop>
			<prop key="org.springframework.beans.TypeMismatchException">/error/mismatchParameter</prop>
			<prop key="org.springframework.web.bind.ServletRequestBindingException">/error/bindException</prop>
			<prop key="org.springframework.dao.DataIntegrityViolationException">/error/integrityViolation</prop>
		</props>
	</property>
</bean>
<!--freemarker配置-->
<bean id="freemarkerViewResolver" class="com.cms.common.web.springmvc.RichFreeMarkerViewResolver">
	<property name="prefix" value="/cms_sys/"/>
	<property name="suffix" value=".html"/>
	<property name="contentType" value="text/html; charset=UTF-8"/>
	<property name="exposeRequestAttributes" value="false"/>
	<property name="exposeSessionAttributes" value="false"/>
	<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
	<property name="templateLoaderPath" value="/WEB-INF"/>
	<property name="freemarkerVariables">
		<map>
			<!--在FCK編輯器中須要用到appBase,以肯定connector路徑。-->
			<entry key="appBase" value="/admin/cms"/>
			<!--後臺管理權限控制-->
			<entry key="cms_perm" value-ref="cms_perm"/>
			<entry key="text_cut" value-ref="text_cut"/>
			<entry key="html_cut" value-ref="html_cut"/>
		</map>
	</property>
	<property name="freemarkerSettings">
		<props>
			<prop key="template_update_delay">0</prop>
			<prop key="defaultEncoding">UTF-8</prop>
			<prop key="url_escaping_charset">UTF-8</prop>
			<prop key="locale">zh_CN</prop>
			<prop key="boolean_format">true,false</prop>
			<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
			<prop key="date_format">yyyy-MM-dd</prop>
			<prop key="time_format">HH:mm:ss</prop>
			<prop key="number_format">0.######</prop>
			<prop key="whitespace_stripping">true</prop>
			<prop key="auto_import">/ftl/cms/index.ftl as p,/ftl/spring.ftl as s</prop>
		</props>
	</property>
</bean>

<!--見89行-->
<context:annotation-config/>

<!--action注入配置文件-->
<import resource="admin-action.xml"/>

對於:admin-action.xml

<bean id="customerAct" class="com.customer.action.CustomerAct"/>
<bean id="basedataAct" class="com.customer.action.BasedataAct"/>
<bean id="employeeAct" class="com.customer.action.EmployeeAct"/>
<bean id="projectAct" class="com.customer.action.ProjectAct"/>
<bean id="productAct" class="com.customer.action.ProductAct"/>

每個action都要用Controller註解,其中使用的manager屬性用Autowired註解,每個方法都要用RequestMapping註解 每個manager都要用Service,Transactional註解

相關文章
相關標籤/搜索