【问题标题】:How can i remove html tags from a text and store it in EL?如何从文本中删除 html 标签并将其存储在 EL 中?
【发布时间】:2016-01-06 22:40:19
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: javascript html json atg atg-dynamo


    【解决方案1】:

    最简单的方法是开发自己的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"/>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2016-04-06
      • 2019-03-04
      相关资源
      最近更新 更多