web.xml-5大配置 <完整>
1,核心控制器
2,Spring.xml配置路径
3,跟随启动—启动Tomcat的就是就会创建servlet
4,过滤器/
5,Post请求乱码
:DispatcherServlet 核心控制器,分配任务,让所有请求都经过该控制器
:contextConfigLocationl, Spring属性,它会去找相应的配置文件
:<load-on-startup> 该servlet会跟着容器启动而创建对象,里面的值就是启动顺序
:dispatcherServlet 过滤器,关联核心控制器名,给该控制器,提供过滤条件
:CharacterEncodingFilter post请求乱码设置,
<?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>
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.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,post请求乱码
<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.xml核心配置文件–
SpringMVC5大组件<完整>
1,SpringMVC 扫描包
2,SpringMVC 注解生效
3,SpringMVC 支持静态资源
4,视图解析器
5,上传解析器
:context:component-scan 扫描包标签,生成该标签后,可不用在单独配置<Bean对象>,配合使用 !@Controller注解. 该注解就相当于一个<Bean>对象
:<mvc:annotation-driven /> 注解生效,因配置了支持静态资源的配置,该配置会令上面的注解使其效应,所有要配置生效标签
:<mvc:default-servlet-handler />支持静态资源,因配置了表达式,而该表达式中,的过滤条件 "/"会覆盖掉 Xxx.xml的配置,令其不能访问静态资源,设置该白标签即可访问
:InternalResourceViewResolver 视图解析对象,该对象可设置 请求返回 前缀/后缀
:CommonsMultipartResolver 上传解析对象,可设置上传文件最大值
maxUploadSize 文件最大支持,可自主设置
特例:引入其他Spring配置文件
方式1:
<import resource="classpath:applicationContext.xml"/>
方式2:
web.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"></context:component-scan>
2:注解生效
<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>
ps:需要的消息头,自己添加
==========================================================
JDBC连接池配置<完整>
1:classpath 告知jdbc路径
2:BasicDataSource配置连接池对象
3:配置四大金刚<驱动,url,username,password>
4:配置JdbcTemplate 专门为咱们准备了一个类,用来完成数据库的操作
5:JdbcTemplate :jDBC的模板(公共的代码已经完成)
<!-- 1.告知jdbc.配置文件路径 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2.配置dataSource(连接池) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 3.配置四大金刚 -->
<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>