首先在jsp中导入:
<jsp:directive.page import="org.springframework.web.context.WebApplicationContext"/>
然后可以调用Spring容器管理的Bean了(这里实例调用的是Service对象):
[c-sharp]
- < %
- WebApplicationContext
context = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); - TestService
service = (TestService)context.getBean("testService"); - %>
如果是在标签类中:
WebApplicationContext context = (WebApplicationContext)this.pageContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);