欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

JSP页面中调用Spring Bean

发布时间:2024/9/19 javascript 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JSP页面中调用Spring Bean 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前言

  • spring boot 2.0.3.RELEASE
  • spring mvc view : jsp

JSP页面中调用Spring Bean

  • 在 JSP 页面获得 WebApplicationContext 。
  • 通过 WebApplicationContext 获取 Bean 的实例。
  • 通过 Bean 的实例调用 Bean 内的方法。
  • 在 JSP 页面获得 WebApplicationContext

    方法1

    <%@page import="org.springframework.web.context.WebApplicationContext" %> <%@page import="org.springframework.web.context.support.WebApplicationContextUtils" %> ... WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); ...

    方法2

    <%@page import="org.springframework.web.context.WebApplicationContext" %> ... WebApplicationContext context = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); ...

    示例

    <%@page import="org.springframework.web.context.WebApplicationContext" %> <%@page import="org.springframework.web.context.support.WebApplicationContextUtils" %> <%@page import="my.package.service.IArticleService" %> ... WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); IArticleService articleService= (IArticleService)context.getBean("articleService"); ...

    参考

    https://docs.spring.io/spring-framework/docs/5.0.6.RELEASE/spring-framework-reference/web.html#web-integration-common

    总结

    以上是生活随笔为你收集整理的JSP页面中调用Spring Bean的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。