2017-05-25 78 views
0

我们的项目是基于Java的,所以我有这样的,例如:如何使用时删除HTML标签的<bean:写/>

<meta name="description" content="<bean:write name="htmlHeadDescription" ignore="true"/>"/>

的事情是,这个htmlHeadDescription似乎是HTML和不是纯文字。

所以 - 有没有办法直接剥离HTML标签?

+0

你不能直接做,但转义html内容不会返回纯文本。 –

回答

0

我做到了,有点像manually ......像这样:

而是具有:

<meta name="description" content="<bean:write name="htmlHeadDescription" ignore="true"/>"/> 

现在我有这样的:

<% 
    // Remove any bad stuff - ONLY for the meta description tag .. 
    request.setAttribute("htmlDescriptionStripped", 
     request.getAttribute("htmlHeadDescription").toString() 
      // remove any eventual remaining HTML tags .. 
      .replaceAll("\\<(.*?)>","") 
      // make everything on one row .. 
      .replaceAll("\\s+"," ") 
      // remove any eventual CSS code .. 
      .replaceAll("(@media|.|#)(.*)\\{(.*)\\}","") 
    ); 
%> 
<meta name="description" content="<bean:write name="htmlDescriptionStripped" ignore="true"/>"/> 

它看起来像它的工作:)