We are excited to get our new site up and running. (We apologize if some of you came to see us previously and we had nothing but fake Latin plastered all over the site. <wink>) We are slowly but surely pulling our content together. The discovery process of finding our theme, our niche, our message to you–as communicated by this site–has been enlightening.
How?
We looked at our combined expertise and realized we had A LOT to offer in a lot of different areas. If you go to our “Welcome” page, you’ll see a bullet-by-bullet list of our in-house skills. We guarantee that at some point or another you will find very helpful information, analysis, and advice in all of, or more then what’s listed there.
You can look forward to enriching content being added post by post, experience by experiene, day by day. Check back often and join our community so that we can “piece i.t. together”.
Excentrix Web is born! We are A-LIVE!!!
Greetings Grail’ngs,
I have been SO, SO, SO impressed with Grails since I started learning it less then two weeks ago. It’s almost as easy to learn and implement as PHP. Of course, it lends itself well to the Java/J2EE paradigm since it is a subset of the Java language–just in the form of a scripting language–but in so doing it still brings along some of the complexity inherent in moving a J2EE project.
At any rate, I wanted to cut to the chase about the main thrust of this post: Grails and RESTful services through content negotiation.
From the start of using Grails, I wanted to expose my service layers as a group of Grails RESTful services so that various types of clients–Flex/Flash, PHP, JSP, and the like–can get what they need in a uniform manner. So, I found this article here which gave me a good start. However, it was outdated insofar as content negotiation and the scaffolding goes. I’ve attached an updated Controller.groovy file (that, after you do “grails install-templates”, comes under src/templates/scaffolding/). It’s not exactly like what Tomas has written as I had to do some debugging/updating of his code to get it to work with Grails 1.0.x and later.
Let me know if it works for you!
Controller.groovy
*My ISP is having perm problems so I’ve cut/pasted Controller.groovy below:
/**********BEGIN CODE*************/
import grails.converters.*
<%=packageName ? “package ${packageName}\n\n” : ”%>class ${className}Controller {
def index = { redirect(action:list,params:params) }
// the delete, save and update actions only accept POST requests
// static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def list = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
withFormat{
html{ [ ${propertyName}List: ${className}.list( params ), ${propertyName}Total: ${className}.count() ] }
xml{ render ${className}.list( params ) as XML }
}
}
def show = {
withFormat{
html{
def ${propertyName} = ${className}.get( params.id )
if(!${propertyName}) {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
else { return [ ${propertyName} : ${propertyName} ] }
}
xml{
if(params.id && ${className}.get(params.id)) {
def c = ${className}.get( params.id )
render c as XML
}
else {
def all = ${className}.list( params )
render all as XML
}
}
}
}
def delete = {
def ${propertyName} = ${className}.get( params.id )
if(${propertyName}) {
try {
${propertyName}.delete()
flash.message = “${className} \${params.id} deleted”
redirect(action:list)
}
catch(org.springframework.dao.DataIntegrityViolationException e) {
flash.message = “${className} \${params.id} could not be deleted”
redirect(action:show,id:params.id)
}
}
else {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
}
def edit = {
def ${propertyName} = ${className}.get( params.id )
if(!${propertyName}) {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
else {
return [ ${propertyName} : ${propertyName} ]
}
}
def update = {
def ${propertyName} = ${className}.get( params.id )
if(${propertyName}) {
if(params.version) {
def version = params.version.toLong()
if(${propertyName}.version > version) {
<%def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className)%>
${propertyName}.errors.rejectValue(”version”, “${lowerCaseName}.optimistic.locking.failure”, “Another user has updated this ${className} while you were editing.”)
render(view:’edit’,model:[${propertyName}:${propertyName}])
return
}
}
${propertyName}.properties = params
if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
flash.message = “${className} \${params.id} updated”
redirect(action:show,id:${propertyName}.id)
}
else {
render(view:’edit’,model:[${propertyName}:${propertyName}])
}
}
else {
flash.message = “${className} not found with id \${params.id}”
redirect(action:edit,id:params.id)
}
}
def create = {
def ${propertyName} = new ${className}()
${propertyName}.properties = params
return ['${propertyName}':${propertyName}]
}
def save = {
def ${propertyName} = new ${className}(params)
if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
flash.message = “${className} \${${propertyName}.id} created”
redirect(action:show,id:${propertyName}.id)
}
else {
render(view:’create’,model:[${propertyName}:${propertyName}])
}
}
}
/*********END CODE**********/
NOTE: Please keep in mind there are no guarantees with this software. Use at your own risk!
We are in need of a Web Developer and Web Designer.
See our
CraigsList Web Developer Posting: http://saltlakecity.craigslist.org/eng/1151489301.html
CraigsList Web Designer Posting: http://saltlakecity.craigslist.org/web/1151470910.html
Or go to our “Contact Us” page.
Excentrix Web, Inc.
Address
1818 W 950 S
Springville, UT 84663
Phone
Main: 801-709-4401
Fax: 801-515-8818
Please pardon our dust as we as a company just came into existence and consequently are still in mid-implementation of our new website. We are excited to offer current and future clients our expertise in various fields of Information Technology. The purpose of this website will be to distill the knowledge we’ve garnered over the [...]
We are excited to get our new site up and running. (We apologize if some of you came to see us previously and we had nothing but fake Latin plastered all over the site. <wink>) We are slowly but surely pulling our content together. The discovery process of finding our theme, our niche, our [...]