简体   繁体   English

如何从文本中删除html标签并将其存储在EL中?

[英]How can i remove html tags from a text and store it in EL?

i am getting data using atg dsp:valueof tag with 'valueishtml' property set to true. 我正在使用atg dsp:valueof标记获取数据,并将'valueishtml'属性设置为true。 I need to pass this data retrieved ie without any html tags to a json variable via EL. 我需要通过EL将检索到的数据(即没有任何html标记)传递给json变量。 Can someone guide me how this can be done? 有人可以指导我如何做到这一点吗? . Below is an example of what i need to do.Please note this is not a code. 以下是我需要执行的示例。请注意,这不是代码。

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

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

currently "product.data" contains html tags which are getting passed to json. 当前,“ product.data”包含将传递给json的html标签。 need json data without any html tags. 需要没有任何html标记的json数据。

TIA TIA

The simplest way to do this would be to develop your own tagconverter . 最简单的方法是开发自己的tagconverter A simple way to achieve is as follow: 一个简单的实现方法如下:

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();
    }

}

This is then referenced via a component properties file, containing the pattern: 然后通过包含该模式的组件属性文件进行引用:

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

Obviously this assumes that everything between a starting '<' and ending '>' should be removed. 显然,这假定应该删除开始的“ <”和结束的“>”之间的所有内容。 You can work on the RegEx yourself to find a better pattern. 您可以自己处理RegEx,以找到更好的模式。

You should also register the TagConverter in the Initial.properties 您还应该在Initial.properties中注册TagConverter。

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

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

Now you should be able to use it as you intended. 现在,您应该可以按预期使用它了。

var mydatawithouthtml = '<dsp:valueof param="product.data" converter="striphtml"/>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM