web.xml
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>
<!-- 前端控制器 -->
<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:application.xml</param-value>
</init-param>
<!-- 设置该Servlet的加载顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置监听器,加载其他的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器,处理post请求的中文乱码 -->
<filter>
<filter-name>EncodeFilter</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>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 对jsp进行全局配置 -->
<jsp-config>
<jsp-property-group>
<!-- 配置拦截所有的jsp页面 -->
<url-pattern>*.jsp</url-pattern>
<!-- 可以使用EL表达式 -->
<el-ignored>false</el-ignored>
<!-- 不能在页面使用脚本 -->
<scripting-invalid>true</scripting-invalid>
<!-- 加载一个全局的jsp配置文件 -->
<include-prelude>/WEB-INF/jsp/taglib.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<!-- 配置全局的错误页面 -->
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
application.xml
<!-- spring 可以自动去扫描 base-package 下面的包或者子包下面的java文件,如果扫描到spring的相关注解的类,则吧该类注解为spring的bean-->
<context:component-scan base-package="com.lds"/>
<!-- 当没有配置处理器映射器和处理器适配器时,spring会使用默认的处理器和适配器处理请求 -->
<mvc:annotation-driven>
<!-- 等同于在处理器适配器中添加对象与json的转换器 -->
<!-- register-defaults=false,不适用SpringMVC自带的转换器 -->
<mvc:message-converters register-defaults="false">
<!--spring消息转换器 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
<!--解决@Responcebody中文乱码问题 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 添加FastJson的转换器 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="charset" value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<!--顺序保持这样,避免IE下载出错 -->
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<!-- 对FastJSON进行配置 -->
<property name="fastJsonConfig" ref="fastJsonConfig"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--fastJsonConfig -->
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<!--默认编码格式 -->
<property name="charset" value="UTF-8" />
<!-- 指定FastJSON可以对哪些格式的信息进行转换 -->
<property name="serializerFeatures">
<list>
<value>WriteNullListAsEmpty</value>
<value>WriteDateUseDateFormat</value>
<value>PrettyFormat</value>
<value>WriteMapNullValue</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullListAsEmpty</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
<!--视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Spring框架默认情况下,是对所有的资源(各种类,.html,.jsp,图片,.xml等)进行过滤.如果我们想放行静态资源,此时的解决方式有2步: -->
<!-- 1.在web.xml文件中的url-pattern写/ -->
<!-- 2.添加如下配置 -->
<mvc:default-servlet-handler/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 文件上传的最大值 -->
<property name="maxUploadSize">
<value>10482880</value>
</property>
<!-- 限定文件上传时的编码 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
mybatis.xml
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
<settings>
<setting name="callSettersOnNulls" value="true" />
</settings> -->
<!-- 这个Bean要在Dao类中引用 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- MapperScannerConfigurer:用来批量创建代理对象.需要指明代理类的父类接口(UserMapper接口) -->
<!-- 该类生成的代理对象的名称是Mapper接口的名称,但是首字母小写,userMapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- basePackage:指明代理对象的父接口的位置. -->
<property name="basePackage" value="com.lds.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
db.properties
jdbc.driverclass=
jdbc.url=
jdbc.username=
jdbc.password=