2017-05-25 30 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"/>"/> 

它看起來像它的工作:)