如何评估动态/嵌套Spring属性占位符表达式? [英] How to evaluate a dynamic/nested Spring property placeholder expression?

查看:244
本文介绍了如何评估动态/嵌套Spring属性占位符表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理JSP标签.这是行,开始循环浏览模型中的项目:

I'm working on a JSP tag. Here is the old line that starts looping through items in a model:

<c:forEach var="toc" items="${requestScope[formKey].model.sharingTocs}">

但是代码已经重构,因此模型路径(上面的model.sharingTocs)现在是动态的,而不是固定的.现在,它通过JSP @attribute传递到标签中:

But the code has been refactored so the model path (model.sharingTocs above) is now dynamic rather than fixed. It is now passed into the tag via a JSP @attribute:

<%@attribute name="path" required="true"%>

所以${path}现在计算为"model.sharingTocs".

现在如何分配items?

推荐答案

好.好问题.

这是一个解决方案:编写自定义jstl标记以评估bean的属性表达式:

<mytag:eval bean="${requestScope['formKey']}" propertyExpression = "${path}" var="items" />

还有ForEach:

<c:forEach var="toc" items="${items}">
</c:forEach>

mytag:eval JSTL标签的示例代码(经典模型)

public class EvalTag extends TagSupport
{

    private Object bean;
    private String propertyExpression; //Ex: 'model.sharingTocs'
    private String var;

    //............


    @Override
    public int doEndTag() throws JspException {
        try {

            // Use reflection to eval propertyExpression ('model.sharingTocs') on the given bean

            Object propObject = SomeLibs.eval ( this.bean, this.propertyExpression);

            this.pageContext.getRequest().setAttribute(this.var, propObject);

            // You can add propObject into Other scopes too.

        } catch (Exception ex) {
            throw new JspTagException(ex.getMessage(), ex);
        }
        return EVAL_PAGE;
    }

      //............
      // SETTERS here
 }

可用于评估属性的lib表示bean的表达是Apache bean utils.

http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#standard.nested

这篇关于如何评估动态/嵌套Spring属性占位符表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆