JSP页面调用Spring容器注入的Bean

首先在jsp中导入:
<jsp:directive.page import="org.springframework.web.context.WebApplicationContext"/>

然后可以调用Spring容器管理的Bean了:
<%  
WebApplicationContext context = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  
TestService service = (TestService)context.getBean("bean ID");  
%>

猜你喜欢

转载自lucky16.iteye.com/blog/1740900
今日推荐