在servlet容器中使用spring框架时spring的上下文(WebApplicationContext)被在保存在 ServletContext中。在servlet、filter或jsp标签中获取spring bean的关键在于如何获取ServletContext。
servlet中:
public void init(ServletConfig config){
servletContext= config.getServletContext();
}
filter中:
public void init(FilterConfig config) throws ServletException {
ServletContext servletContext =config.getServletContext();
}
jsp taglib中:
ServletContext servletContext = this.pageContext.getServletContext();
spring的上下文(WebApplicationContext)存在ServletContext以 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE为key的属性中,可通过如下方式获取:
Object ob = servletContext .getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
WebApplicationContext context = (WebApplicationContext) ob;
在WebApplicationContext中获取bean:
protected Object getBean(String name){
return this.getWebApplicationContext().getBean(name);
}
一个抽像的servlet基类:
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.Servlet;
import org.springframework.web.context.WebApplicationContext;
public abstract class BaseServlet implements Servlet {
protected ServletContext servletContext;
public void init(ServletConfig config){
servletContext= config.getServletContext();
}
public ServletContext getServletContext(){
return servletContext;
}
public void destroy(){
}
public String getServletInfo(){
return "";
}
protected WebApplicationContext getWebApplicationContext()
throws RuntimeException{
Object ob = servletContext.getAttribute(WebApplicationContext
.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
WebApplicationContext context = (WebApplicationContext) ob;
return context;
}
protected Object getBean(String name){
return this.getWebApplicationContext().getBean(name);
}
public ServletConfig getServletConfig(){
return null;
}
}
分享到:
相关推荐
绝对可以解决这个这个问题。Struts初学者绝对可以受用的方法。
Servlet MVC taglib JSON Android 分页
11 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 12 13 14 <prop key="/hello.do">helloController 15 16 17 </bean> 18 19 <bean id=...
使用taglib类对多媒体文件进行解析,以获取多媒体文件的作者、流派、大小等。
org.springframework.web.filter.CharacterEncodingFilter </filter-class> <param-name>encoding <param-value>UTF-8 </filter> <filter-mapping> <filter-name>encodingFilter</filter-name...
用pager-taglib进行分页时,如果出现中文参数,由于pager-taglib默认是用ISO8859-1编码后进行参数传递,故会出现乱码现象。本人下载源码后,将编码的语句注释掉,即不进行编码,解决了中文乱码问题。改写后的代码已...
j2ee中struts+hibernate+spring+pager-taglib分页实例 这套解决方案,是按照MVC的模式来做的,基于STRUTS框架。分为三个部分,表现层用的是JSP,其中用了JSTL和pager taglib做分页;控制层就是用的很传统的Action;...
struts taglib使用示例 struts taglib使用示例 struts taglib使用示例struts taglib使用示例 struts taglib使用示例
文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...
12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 SecurityMockMvcRequestBuilders 109 12.3.1测试基于表单的...
1 什么是Tag 一个tag就是一个普通的java类,它唯一特别之处是它必须继承TagSupport或者BodyTagSupport类。这两个类提供了一些方法,负责jsp页面和你编写的类之间的交互,例如输入,输出。而这两个类是由jsp容器提供...
struts-taglib-1.3.10.jar struts-taglib.jar taglib.jar
修正了中文参数传递乱码问题的pager控件。用于jsp分页显示pager-taglib.jar。直接放到lib文件夹即可使用。
结合taglib更新了之前开发的lMusic,完善了音乐播放器,bug更少
idea需要的taglib标签库包,idea没有自带的taglib标签库,需要手动导入taglib包,并且配置对应的路径,进入“settings”设置选项,然后搜“DTD”然后如下图,在左边的标签选项卡找到“Schemas and DTDS” ,在url的...
taglib-1.11.1 taglib-1.11.1 taglib-1.11.1 taglib-1.11.1 taglib-1.11.1 taglib-1.11.1 taglib-1.11.1
支持中文传输的pager-taglib.jar
jsp中taglib导入http://www.springframework.org/tags发生java.lang.ClassNotFoundException错误
5.5. Spring Security中的访问控制(验证) 5.5.1. 安全和AOP建议 5.5.2. 安全对象和AbstractSecurityInterceptor 5.5.2.1. 配置属性是什么? 5.5.2.2. RunAsManager 5.5.2.3. AfterInvocationManager ...
讲述Taglib 自定义标签