【问题标题】:Call Spring bean method in JSP在 JSP 中调用 Spring bean 方法
【发布时间】:2012-06-07 18:33:22
【问题描述】:

我有一个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);
 }

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    如果您确实希望在 jsp 页面中使用 Layouts 列表,则需要从 spring 控制器中将此列表添加到 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>
    

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

    【讨论】:

    • 我现在有办法使用 servlet 在 JSP 页面中直接调用该函数吗?我使用 JSP 搜索表单传入了 subjectName,我希望该方法只运行它
    • 我可以想到的一种方式,在 servlet 的 http 请求/响应中添加为属性,并使用 jsp 内置的请求/响应对象访问该属性。查看此链接以获取更多信息gulland.com/courses/jsp/objects
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多