1.在使用spring和springmvc,有许多需要注意的地方.
a)spring和springmvc的整合
1)springmvc的全局异常处理器的配置
2)spring和springmvc的扫包
3)springmvc拦截器的配置
4)springmvc的欢迎页面的配置
5)静态资源的拦截
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描-->
<context:component-scan base-package="com.brant"></context:component-scan>
<!--加载mvc注解驱动-->
<mvc:annotation-driven/>
<import resource="classpath:applicationContext-mybatis.xml"/>
<!--配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 告诉springmvc,哪些资源是不需要你处理的(放行) -->
<mvc:resources mapping="/static/**" location="/static/"></mvc:resources>
<mvc:resources mapping="/upload/**" location="/upload/"></mvc:resources>
<!-- 告诉springmvc,设置默认首页地址 -->
<mvc:view-controller path="/" view-name="redirect:/index/showIndex"></mvc:view-controller>
<!--配置全局异常处理器-->
<bean id="sysExceptionResolver" class="com.brant.util.GlobalExceptionHandler"/>
<!-- 配置全局拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器如果拦截的地址一致,那么按照配置的顺序执行 -->
<!-- 配置了一个拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.brant.controller.LoginStateInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制上传文件的大小(字节) -->
<property name="maxUploadSize" value="10000000" />
</bean>
</beans>
. 7)springmvc的cookie的保存路径的设置:https://blog.csdn.net/brantykl/article/details/121576084
. 8)spring请求转发路径的拼接问题
1.在使用相对路径经常会拼接本类的请求路径,而不是项目路径.因此在使用路径或者进行路径拼接时尽量使用绝对路径
2.有关spring和web.xml
a)spring的注解注入和web.xml的过滤器的冲突
1)由于web.xml过滤器的执行顺序是在spring注解注入之前,因此如果想要通过注入获取对应的类是不可以的
2)可通过以下三种方式解决
//第一种
ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
userService =(UserService)act.getBean("userServiceImpl");
//第二种
/*获取并注入的UserServiceImpl*/
WebApplicationContext webApplicationContext =
(WebApplicationContext) request.getSession().getServletContext().
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
userService = webApplicationContext.getBean(UserServiceImpl.class);
//第三种
//使用springmvc拦截器
b)spring的注解注入和web.xml的监听器的冲突
1)由于web.xml监听器的执行顺序是在spring注解注入之前,因此如果想要通过注入获取对应的类是不可以的,可使用以上介绍的第一种或者第二种
c)Tomcat编码格式和过滤器的配置
1)由于tomcat的编码格式默认是iso8859-1,因此即便是使用过滤器或者springmvc的拦截器过滤编码格式,后台依旧会获取乱码.
2)修改tomcat下的conf/server.xml文件
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" useBodyEncodingForURI="true" />