1 :Spring
1.概念
Spring是一款轻量级的框架,基于DI/IOC 于AOP的一款容器框架
DI:依赖注入
IOC:控制反转(和DI差不多)
AOP:面向切面
容器:Spirng,
2:Spring.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
" >
<!--1 扫描包 -->
<context:component-scan base-package="cn.pan.cms"></context:component-scan>
<!--2 读取配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--3,加载DataSource对象,配置4金刚 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--4,配置JdbcTemplate操作数据库 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
3:SpringMVC.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
" >
<!--SpringMVC 5大组件 -->
<!-- 引入其他Sprng文件
<import resource="classpath:applicationContext.xml"/> -->
<!--1,扫描包 -->
<context:component-scan base-package="cn.pan.cms"></context:component-scan>
<!--2,注解生效 ,支持特有SpingMVC注解-->
<mvc:annotation-driven />
<!--3,支持静态资源 -->
<mvc:default-servlet-handler />
<!--4,视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix:前缀 -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- suffix后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!--5,上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>2000000000</value>
</property>
</bean>
</beans>
4:jdbc.properties配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///cms
jdbc.username=root
jdbc.password=XXXXX
5:web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>sping02</display-name>
<!--0 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--1,核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--2,Spring核心文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--3 跟随启动创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--4,过滤器 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--5,请求乱码! -->
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注意事项:
规范命名:
applicationContext-mvc.xml
applicationContext.xml
jdbc.properties