瑕佸啀JSP閲岄潰寰楀埌ApplicationContext闇瑕佽繖涔堝仛錛岄鍏坕mport="org.springframework.web.context.support.*,org.springframework.context.*"
鐒跺悗鍙互閫氳繃濡備綍鍋氭硶錛?/p>
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
榪欐牱灝卞緱鍒頒簡ApplicationContext錛屽氨鍙互鎿嶄綔Spring浜嗐?/p>
JSP鏈潵灝卞彲浠ヨ涓烘槸涓涓猄ervlet錛屾墍浠ヤ嬌鐢╣etServletContext()灝辨槸鐞嗘墍搴斿綋浜嗐?/p>
涓錛歸eb.xml閰嶇疆
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
浜岋細鍦↗SP
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="com.yourcompany.service.CategoryService"%>
<%
//涓婇潰鐨凜ategoryService寮曠敤鏄垜鑷繁鐨勪笢瑗?br />
//applicationContext.xml涓竴瀹氳鏈夊畬鏁寸殑渚濊禆閾撅紝浠巇ataSource鍒癈ategoryService
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
CategoryService cs = (CategoryService) ctx.getBean("CategoryService");
List list =cs.getCategoryDAO().findAll();
%>