簡體   English   中英

如何從文本中刪除html標簽並將其存儲在EL中?

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

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

最簡單的方法是開發自己的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"/>'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM