gpt4 book ai didi

java - 在JSP中调用Spring bean方法

转载 作者:行者123 更新时间:2023-11-30 04:45:59 25 4
gpt4 key购买 nike

我有一个java类

LayoutManager.java,我使用它作为 Sprean Bean 传递到我的 jsp 页面中

 <custom:useSpringBean var="layoutManager" bean="LayoutManager"/>

如何使用 spring bean 从 jsp 内的 LayoutManager.java 调用方法?

我觉得我会使用某种形式的 servlet <% %>,但不确定语法

我要调用该方法

 public Iterable<Layouts> getSpecificLayout(String subjectName)

我现在唯一的 Spring 代码是

 public class UseSpringBean extends SimpleTagSupport
{
public void doTag() throws JspException, IOExceptionP
PageContext pageContext = (PageContext)getJspContext();
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
pageContext.setAttribute(var, springContext.getBean(bean), PageContxt.PAGE_SCOPE);
}

最佳答案

如果您确实希望在 jsp 页面中使用 Layouts 列表,那么您需要在 Spring Controller 中将此列表添加到 ModelMap 中,以便其可见在jsp页面中。

类似于:

@RequestMapping(value="/getSpecificLayout", method = RequestMethod.GET)
public String getSpecificLayout(Stirng subjectName, ModelMap model){
Iterable<Layouts> layouts = getSpecificLayout(String subjectName);
model.addAttribute("layouts", layouts);
return "listLayouts";
}

在jsp中:

<c:for items="listLayouts" var="layout">
<c:out value="layout.name"/>
</c:for>

(这不是经过测试的代码,只是一个示例。抱歉,代码编辑不起作用,我的意思是我看不到内联编辑器)。

关于java - 在JSP中调用Spring bean方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10937762/

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