在JSP頁面中呼叫Spring容器注入的Bean的2種方法
方法一:
1.首先在jsp中匯入:
<jsp:directive.page import="org.springframework.web.context.WebApplicationContext"/>
2.然後可以呼叫spring容器管理的Bean了(這裡例項呼叫的是CalendarMapper 物件):
Jsp中呼叫spring管理的Bean程式碼如下:
WebApplicationContext context = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
CalendarMapper calendarMapper = (CalendarMapper)context.getBean("calendarMapper");
方法二:
1.在jsp中匯入:
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@page import="org.springframework.web.context.WebApplicationContext" %>
2.然後可以呼叫spring容器管理的Bean了:
WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
CalendarMapper calendarMapper = (CalendarMapper)context.getBean("calendarMapper");