springMVC+spring+hibernate配置

题目中提到的配置,主要有3部

1.配置web.xml

2.配置applicationContext.xml

2.1配置hibernate.cfg.xml

3.配置springMVC

1.看一下web.xml文件,主要是一个监听,一个servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>mbj</display-name>

	<!-- web容器启动的时候,自动加载spring容器的监听 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/applicationContext.xml</param-value>
	</context-param>

	<!--编码连接器-->
	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<!-- 启动spring-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 中央控制器  -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


	<!-- session超时定义,单位为分钟 -->
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
可以选择配置,fliter 主要是完成编码转换。

2.applicationContext.xml。就是配置spring都管理啥,主要就是管理hibernate,在orm包下,还有就是事务配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	">
	<!-- 注解路径 -->
	<context:component-scan base-package="com.ldh" />
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 创建数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${hibernate.connection.driver_class}" />
		<property name="url" value="${hibernate.connection.url}" />
		<property name="username" value="${hibernate.connection.username}" />
		<property name="password" value="${hibernate.connection.password}" />
	</bean>
	<!-- 创建工厂 -->
	<bean name="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					${hibernate.dialect}
				</prop>
				<prop key="hibernate.show_sql">
					${hibernate.show_sql}
				</prop>
				<prop key="hibernate.format_sql">
					${hibernate.format_sql}
				</prop>
			</props>
		</property>
		<!-- 自动扫描 entity -->
		<property name="packagesToScan">
			<list>
				<value>com/ldh/domain</value>
			</list>
		</property>
	</bean>
	<!-- 事务管理 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 事务策略 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				no-rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.SQLException" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- aop映射 -->
	<aop:config>
		<aop:pointcut id="fooServiceOperation" expression="execution(* com.ldh.service.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
	</aop:config>

</beans>

2.1以上配置,只加载了一个jdbc.porperties,没有加载hibernate.cfg.xml

下面给一个加载hibernate.cfg.xml的语句

<!-- 创建SessionFactory,这是spring整合hibernate的核心 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="packagesToScan" value="com.ldh.core.domain" />
		<property name="configLocation">
			<value>classpath:config/hibernate.cfg.xml</value>
		</property>
	</bean>

将一下的工厂代替上面的工厂就可以了。

3.springMVC的配置,这要注意一下,web.xml中servlet的name加上-servlet.xml就是你springMVC配置文件的名称,但在initParam中可以进行设置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
      
    
          
  	<!-- mvc2大组件 -->
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <!-- 注解扫描包 -->  
    <context:component-scan base-package="com.ldh.core.action" />
    
    <!-- 定义视图解析器 -->    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
			<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
			<property name="contentType" value="text/html;charset=UTF-8" />
			<property name="prefix" value="/WEB-INF/View/"/>
			<property name="suffix" value=".jsp"/>
    </bean>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="defaultEncoding" value="UTF-8" />
    	<!-- <property name="maxUploadSize" value="204800" /> -->
    </bean>
    
    
    <!--   静态资源(js/image)的访问   -->
 	<mvc:default-servlet-handler/>
 	<mvc:resources location="/WEB-INF/View/images/" mapping="/images/**"/>
 	<mvc:resources location="/WEB-INF/View/js/" mapping="/js/**"/>
 	<mvc:resources location="/WEB-INF/View/css/" mapping="/css/**"/>
 	<mvc:resources mapping="/easyui/**" location="/WEB-INF/View/easyui/" />
 	<mvc:resources location="/WEB-INF/View/jsp/" mapping="/jsp/**"/>
<!--     
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    
    启动 Spring MVC 的注解功能,完成请求和注解 POJO 的映射
    <bean  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
                </bean>
            </list>
        </property>
    </bean>
    
    <bean id="multipartResolver"     
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"     
          context:defaultEncoding="utf-8" /> -->
</beans>  

注:这里需要注意,你要是把文件放在web-inf下,你要注意了。

你需要配置mvc:resource标签,同时,你还要在web.xml的servlet中将url配置成“/”。这样你的《script》《link》才能找到地址,否则就会包404。

还有很多问题。如果你放在webContent下面就什么都不用管了,mvc:resource标签都删了吧,没啥用。具体这里不详细讲了。

请看:点击打开链接







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值