gpt4 book ai didi

spring - JSP 中的 applicationContext 对象

转载 作者:行者123 更新时间:2023-12-02 01:57:07 26 4
gpt4 key购买 nike

如何在我的 spring 项目中配置以使用 JSTL 检索 jsp 中的 applicationContext 对象。

最佳答案

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

application 是 JSP 定义的 ServletContext。

如果您想检索 bean 并使用 JSTL,您可以执行以下操作:

<%pageContext.setAttribute("bean", context.getBean("myBean"));%>
<c:out value="${bean.property}"/>

但是,仅仅因为您可以,并不意味着您应该这样做。如果您要做的不仅仅是显示 bean 的属性,您可能希望将其放入 Servlet 或其他一些 Controller 中。

此外,您希望使用ApplicationContext 作为在 Controller 和 View 之间传递bean 的方式。

关于spring - JSP 中的 applicationContext 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5154463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com