Spring 配置

Spring控制器配置文件中的各项配置 Servlet.xml

<!-- 指定前端控制器路径  -->
	<context:component-scan base-package="com.lovo.web"/>
	
	<!-- 使用注解方式配置spring mvc -->
	<mvc:annotation-driven/>
	
	<!-- 静态资源仍由Tomcat的默认Servlet进行处理 -->
	<mvc:default-servlet-handler/>
	
	<!-- JSP页面视图解析器  -->
	<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 配置spring的文件上传解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置最大上传文件大小(10M),若想传多大传多大则写-1 -->
		<property name="maxUploadSize" value="10485760"/>
		<!-- 设置默认编码 -->
		<property name="defaultEncoding" value="UTF-8"/>
		<!-- 内存中缓冲区大小 -->
		<property name="maxInMemorySize" value="2048"/>
	</bean>
	
	<!-- 消息转换器  -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="cacheSeconds" value="0"/>
		<property name="messageConverters">
			<list>
				<!-- 通过第三方工具jackson将java对象转换成JSON对象  -->
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
			</list>
		</property>
	</bean>
	
	<!-- 配置spring的模板解析器  -->
	
	<!-- 配置Excel文件解析器  -->
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="10"/>
	</bean>
	<bean id="excelView" class="com.lovo.view.UserListViewResolver"/>
	<bean id="pdfView" class="com.lovo.view.WordListPdfView"/>

	
	<!-- 配置拦截器LoginInterceptor -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/showgoods.do"/>
			<bean class="com.lovo.interceptor.LoginInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors>



 Spring的基本配置文件(applicationContext.xml)

	<!-- 配置注解 可以以大注解的方式托管Bean -->
	<context:annotation-config/>
	<!-- 配置扫描包路径 配置这个之后可以不用配置上面的context配置-->
	<context:component-scan base-package="com.lovo"/>
	
	<!-- 配置数据库连接 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://192.168.1.77:3306/oa"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123"></property>
	</bean>
	
	<!-- 配置会话广场 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="packagesToScan" value="com.lovo.oa" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>
	<!-- 配置事物环境 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager" />

 web.xml配置文件

	<!-- 配置启动IoC容器的监听器 -->
	<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>
	
	<!-- 配置前端控制器 -->
	<servlet>
		<servlet-name>bbs</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>bbs</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 配置编码过滤器 -->
  <filter>
  	<filter-name>coding</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>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>coding</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
	<!-- 配置 让会话延时到视图 -->
	<filter>
		<filter-name>osiv</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>osiv</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置支持RESTful的PUT/DELETE请求的过滤器 -->
	<filter>
		<filter-name>method</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>method</filter-name>
		<servlet-name>bbs</servlet-name>
	</filter-mapping>
	<!-- 配置错误页 -->
	<error-page>
        	<error-code>404</error-code>
		<location>/error.jsp</location>
	</error-page>
	<error-page>
		<error-type>java.lang.Exception</error-type>
		<location>/error.jsp</location>
	</error-page>

 

你可能感兴趣的:(spring)