user9363986
user9363986

Reputation:

How to call my created method on my getProjectLists in order to remove HTML tags from String

This is my getProjectLists action

  def getProjectLists() {
      def currentUser = springSecurityService.currentUser
      def kups = ([['name':'<b>Sample 1</b>'.removeHthml()],
      ['name':'<b>Sample 2</b>']])
  render kups as JSON  
 }

And this is my class and method

class HtmlTagRemove {
    static String removeHthml(String inputStr) {
      return inputStr.replaceAll("<.*?>","")
    }
}

This the error I encountered

No signature of method: java.lang.String.removeHtml() is applicable for argument types: () value: []

FOLLOW-UP (Even though the original question was answered, my follow up question is as follows)

What can I do to make my method callable from a String like: '<b>Sample 1</b>'.removeHtml()?

Upvotes: 0

Views: 58

Answers (2)

Sascha Frinken
Sascha Frinken

Reputation: 3366

Answering the follow-up question:

You could extend the String class. Documentation

​String.metaClass.removeHtml {    
    delegate.replaceAll("<.*?>","")
}

'<b>Sample 1</b>'.removeHtml()

I would use a library like Jsoup to remove html from a string:

Document doc = Jsoup.parse(html)
return doc.text())

Upvotes: 1

Joshua Moore
Joshua Moore

Reputation: 24776

Provided that they are in the same package (otherwise you need to import HtmlTagRemove or quality it with a full package name) you can do this:

def kups = ([['name':HtmlTagRemove.removeHthml('<b>Sample 1</b>')], ['name':'<b>Sample 2</b>']])

The reason being is that removeHtml is a static method.

Upvotes: 1

Related Questions