0

我正在使用 atg dsp:valueof 标记获取数据,其中“valueishtml”属性设置为 true。我需要通过 EL 将检索到的数据(即没有任何 html 标记)传递给 json 变量。有人可以指导我如何做到这一点吗?. 下面是我需要做的一个例子。请注意这不是代码。

var mydatawithouthtml = <dsp:valueof param="product.data" valueishtml="true"/>

<json:property name="data" value="${mydatawithouthtml}" />

目前“product.data”包含传递给 json 的 html 标签。需要没有任何 html 标签的 json 数据。

TIA

4

1 回答 1

0

最简单的方法是开发自己的tagconverter. 一个简单的实现方法如下:

package com.acme.tagconverter;

import java.util.Properties;
import java.util.regex.Pattern;

import atg.droplet.TagAttributeDescriptor;
import atg.droplet.TagConversionException;
import atg.droplet.TagConverter;
import atg.droplet.TagConverterManager;
import atg.nucleus.GenericService;
import atg.nucleus.ServiceException;
import atg.servlet.DynamoHttpServletRequest;

public class StripHTMLConverter extends GenericService implements TagConverter {

    private Pattern tagPattern;

    @Override
    public void doStartService() throws ServiceException {
        TagConverterManager.registerTagConverter(this);
    }

    public String convertObjectToString(DynamoHttpServletRequest request, Object obj, Properties attributes) throws TagConversionException {
        return tagPattern.matcher(obj.toString()).replaceAll("");
    }

    public Object convertStringToObject(DynamoHttpServletRequest request, String str, Properties attributes) throws TagConversionException {
        return str;
    }

    public String getName() {
        return "striphtml";
    }

    public TagAttributeDescriptor[] getTagAttributeDescriptors() {
        return new TagAttributeDescriptor[0];
    }

    public void setTagPattern(String tagPattern) {
        this.tagPattern = Pattern.compile(tagPattern);
    }

    public String getTagPattern() {
        return tagPattern.pattern();
    }

}

然后通过包含模式的组件属性文件引用它:

$class=com.acme.tagconverter.StripHTMLConverter
tagPattern=<[^>]+>

显然,这假设应该删除开始 '<' 和结束 '>' 之间的所有内容。您可以自己研究 RegEx 以找到更好的模式。

您还应该在 Initial.properties 中注册 TagConverter

$class=atg.nucleus.InitialService
$scope=global

initialServices+=\
    /com/acme/tagconverter/StripHTMLConverter 

现在您应该可以按照您的意愿使用它了。

var mydatawithouthtml = '<dsp:valueof param="product.data" converter="striphtml"/>'
于 2016-01-07T06:20:26.640 回答