Monday, September 9, 2013

Grails : Displaying Error Message on View which is thrown from the Service Layer

In your controller class, enclose the code that calls the service in a try-catch block.

Example:
try {

def searchResult = User.getUserByName(params.name)

if(searchResult != null)
render(view: "/admin/user/list", model: [userInstanceList: searchResult?.results, userInstanceTotal: searchResult?.total])

else{

flash.message = message(code: 'user.nonfound.message', default: "No items found.")
render(view: "/admin/user/list", model: [userInstanceList: [], userInstanceTotal: 0])
}
} catch (e) {

flash.message = message(code: 'user.nonfound.message', default: "No items found.")

render(view: "/admin/user/list", model: [userInstanceList: [],userInstanceTotal: 0])
}