一、获取servletContext springMvc获取servletContext
- WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
- ServletContext servletContext = webApplicationContext.getServletContext();
二、获得serveltContext的方式
ServletContext servletContext2 = ContextLoaderListener.getCurrentWebApplicationContext().getServletContext();
三、获得项目工程所在的物理地址
String home = servletContext.getRealPath("/");
四、用接口类WebApplicationContext读取配置文件
private WebApplicationContext wac;
wac =WebApplicationContextUtils.getRequiredWebApplicationContext(
this.getServletContext());
wac = WebApplicationContextUtils.getWebApplicationContext(
this.getServletContext());
JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
其中,jdbcTemplate为spring配置文件中的一个bean的id值。
五、ContextLoaderListener的作用
在 web.xml中,配置了ContextLoaderListener,其完整路径为org.springframework.web.context.ContextLoaderListener。我的理解是,这是整个项目加载的监听器,启动容器时,就会默认执行它实现的方法,自动装配contextConfigLocation指定的配置信息。在ContextLoaderListener 中关联了ContextLoader 这个类,所以整个加载配置过程由ContextLoader 来完成。
如下为配置实例:
<!--Spring集成Web环境的通用配置;一般用于加载除Web层的Bean(如DAO、Service等),以便于与其他任何Web框架集成。
contextConfigLocation:表示用于加载Bean的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml,
classpath*:applicationContext-cti.xml
classpath*:applicationContext-activiti.xml
classpath*:applicationContext-shiro.xml
classpath*:applicationContext-fullindex.xml
classpath*:applicationContext-timer.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
六、DispatchServlet的配置及作用
DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
4、通过ViewResolver解析逻辑视图名到具体视图实现;
5、本地化解析;
6、渲染具体的视图等;
7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。
从以上我们可以看出DispatcherServlet主要负责流程的控制(而且在流程中的每个关键点都是很容易扩展的)。
DispatchServlet的职责,主要配置在contextConfigLocation指定的配置文件中。
DispatchServlet在web.xml中的配置如下:
<!--DispatcherServlet主要用作职责调度工作,本身主要用于控制流程 -->
<!--该DispatcherServlet默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”。 -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
<!--或者以这种方式,指定配置信息<param-value>classpath*:spring/spring-mvc.xml</param-value> -->
</init-param>
<!--表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。 -->
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
七、字符编码过滤器
Spring中的字符编码过滤器(CharacterEncodingFilter),可以很方便的为我们解决项目中出现的中文乱码问题;
使用方法也很简单,只需在web.xml文件中配置一下该过滤器,设置两个参数(encoding和forceEncoding)即可。
如:<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>
相关推荐
Spring基础教学视频,四天课时,视频很清楚讲的很仔细
Spring基础概念,简单的描述javaSpring框架概述,SpringAOP,Spring IOC
Spring基础培训ppt
Spring基础教程.pdf
Spring基础知识汇总 Java开发必看
spring基础教程spring基础教程spring基础教程spring基础教程
《Spring基础进阶技巧200篇》是一本关于Spring框架的资源,它提供了200篇文章,涵盖了Spring框架的基础知识和进阶技巧。这本资源可以帮助开发人员更好地理解和应用Spring框架,提升开发效率和代码质量。 该资源主要...
Spring基础:Spring AOP简单使用
springweb开发的基础jar包
Spring基础jar包,可以搭建Spring环境,运行使用基本功能
Spring框架的五个jar包,其中有Spring-beams、Spring-Context、Spring-Core、commons-logging、Spring-expression
北大青鸟spring基础课件,讲解spring的基础
spring基础示例,包含基本的bean装配和注入,采用原生bean管理、xml配置和注解相对比的方式示例。
spring 基础入门 经典spring 基础入门 经典spring 基础入门 经典spring 基础入门 经典spring 基础入门 经典
Spring框架搭建必导jar包
java Spring基础教程
spring基础操作教程spring基础操作教程spring基础操作教程spring基础操作教程spring基础操作教程