1、首先配置spring 启动IOC容器的Listener
<!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>2、配置前端dispatchServlet
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 此处指向的的是SpringMVC的配置文件 --> <param-value>classpath:META-INF/spring-mvc.xml</param-value> </init-param> <!--配置容器在启动的时候就加载这个servlet并实例化--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>3、编码格式
<!-- 字符集过滤 --> <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>1、设置自动扫描的包
<!-- 启动注解驱动的spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <context:component-scan base-package="com.test" />2、配置视图解析器
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="views/" /> <!-- 前缀 --> <property name="suffix" value=".jsp" /> <!-- 后缀 --> </bean>3、处理MVC静态资源的
<!--这里是对静态资源的映射--> <mvc:resources mapping="/js/**" location="/resources/js/" /> <mvc:resources mapping="/css/**" location="/resources/css/" /> <mvc:resources mapping="/img/**" location="/resources/img/" />4、配置事务
<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
5、配置springData
<!-- 配置 SpringData --> <jpa:repositories base-package="com.atguigu.sssp" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>