If our Grails application renders XML or JSON output we can set configuration properties to enable pretty printing. This can be useful in for example in the development environment where we enable pretty printing and disable it for other environments. We set the configuration property grails.converters.default.pretty.print with the value true to enable pretty printing for both XML and JSON output. If we only want to pretty print XML we use the property grails.converters.xml.pretty.print and for JSON we use grails.converters.json.pretty.print.
First we look at the XML and JSON output when we don't enable pretty printing for a simple book resource:
Continue reading →
We can group URL mappings defined in grails-app/conf/UrlMappings.groovy using the group() method defined for the URL mapping DSL. The first argument is the first part of the URL followed by a closure in which we define mappings like we are used to.
Suppose we have defined the following two mappings in our UrlMappings.groovy file, both starting with /admin:
Continue reading →
Since Grails 2.3 we can use the url-mappings-report command to get a nice report of the URL mappings we have defined in our application. Also implicit mappings created for example by using the resources attribute on a mapping definition are shown in the report. This report is very useful to see which URLs are exposed by your application and how they map to controllers.
Suppose we have the following grails-app/conf/UrlMappings.groovy with a couple of mappings:
Continue reading →
In Grails we can convert a request parameter to a type directly. We must then use the int(), short(), byte(), long(), double(), float(), boolean() or list() methods that are added to the params object available in our controllers.
Since Grails 2.3 we can also pass a default value, which is used when the request parameter is not set. In the following controller we use the double() method and define a default value of 42.0.
Continue reading →
Since Grails 2.3 all ${} expression output is automatically escaped on GSPs. This is very useful, because user input is now escaped and any HTML or JavaScript in the input value is escaped and not interpreted by the browser as HTML or JavaScript. This is done so our Grails application is protected from Cross Site Scripting (XSS) attacks.
But sometimes we do want to output unescaped HTML content in the web browser. For example we generate the value ourselves and we know the value is safe and cannot be misused for XSS attacks. In Grails 2.3 we can use a new raw() method in our GSPs, tag libraries or controllers. The method will leave the content unchanged and return the unescaped value to be displayed. Alternatively we can use encodeAsRaw() on the content we want to leave unescaped. Finally the encodeAs tag accepts Raw or None as values for the attribute codec and will return the unescaped value.
Continue reading →
Since Grails 2.3 it is very easy to define RESTful URL mappings for a controller. We can use the resources and resource attribute and use a controller name as the value. Grails will then automatically create a couple of URL mappings. Suppose we use the following mapping in grails-app/conf/UrlMappings.groovy: "/api/users"(resources: 'user'), then the following mappings are automatically created:
"/api/users/create"(controller: 'user', action: 'create', method: 'GET')
"/api/users/(*)/edit"(controller: 'user', action: 'edit', method: 'GET')
"/api/users(.(*))?"(controller: 'user', action: 'save', method: 'POST')
"/api/users(.(*))?"(controller: 'user', action: 'index', method: 'GET')
"/api/users/(*)(.(*))?"(controller: 'user', action: 'delete', method: 'DELETE')
"/api/users/(*)(.(*))?"(controller: 'user', action: 'update', method: 'PUT')
"/api/users/(*)(.(*))?"(controller: 'user', action: 'show', method: 'GET')
Continue reading →
In a Grails application we can organize our controllers into packages, but if we use the same name for multiple controllers, placed in different packages, then Grails cannot resolve the correct controller name. Grails ignores the package name when finding a controller by name. But with namespace support since Grails 2.3 we can have controllers with the same name, but we can use a namespace property to distinguish between the different controllers.
We can add a new static property to a controller class with the name namespace. The value of this property defines the namespace. We can then write new URL mappings in the grails-app/conf/UrlMappings.groovy file and use the namespace value as a mapping attribute.
Continue reading →
Grails has a built-in URL constraint to check if a String value is a valid URL. We can use the constraint in our code to check for example that the user input http://www.mrhaki.com is valid and http://www.invalid.url is not. The basic URL validation checks the value according to standards RFC1034 and RFC1123. If want to allow other domain names, for example server names found in our internal network, we can add an extra parameter to the URL constraint. We can pass a regular expressions or a list of regular expressions for patterns that we want to allow to pass the validation. This way we can add IP addresses, domain names and even port values that are all considered valid. The regular expression is matched against the so called authority part of the URL. The authority part is a hostname, colon (:) and port number.
In the following sample code we define a simple command object with a String property address. In the constraints block we use the URL constraint. We assign a list of regular expression String values to the URL constraint. Each of the given expressions are valid authorities, we want the validation to be valid. Instead of a list of values we can also assign one value if needed. If we don't want to add extra valid authorities we can simple use the parameter true.
Continue reading →
In Grails we can define properties for services, controller, taglibs, Spring components and other components in the Spring application context through configuration. This is called property overriding in Spring terminology. This feature is very useful to define or override property values on components in the application context. We can define the property values in a beans{} block in Config.groovy. We can also load external configuration files, like property files and define property values in those.
With property overriding we don't have to look for property values via an injected GrailsApplication object and the config property of GrailsApplication. The code using the property value is now much cleaner and easier to test.
Continue reading →
Today a colleague asked a group of front-end developers how he would create a superelipse. His current solution was to use a svg mask to remove all non essential visual information. This solution however had a setback, because we used a mask to shield the edges we had no real transparency. Thus we were unable to effectively use it on more graphic backgrounds. I however thought it should be able to use canvas to provide the solution. The code below is my solution.
<canvas id="myCanvas" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var image = canvas.getContext('2d');
var width = canvas.width;
var height= canvas.height;
var img = document.createElement('IMG');
var shape = {
beginPath : function(node) {
image.beginPath();
image.moveTo(
node.x,
node.y
);
},
createCurve : function(curve, node) {
image.quadraticCurveTo(
curve.x,
curve.y,
node.x,
node.y
);
},
node : {
top_left : {
x : ((width/100)*20),
y : ((height/100)*5)
},
top_right : {
x : ((width/100)*80),
y : ((height/100)*5)
},
right_top :{
x : ((width/100)*95),
y : ((height/100)*20)
},
right_bottom :{
x : ((width/100)*95),
y : ((height/100)*80)
},
bottom_right : {
x : ((width/100)*80),
y : ((height/100)*95)
},
bottom_left : {
x : ((width/100)*20),
y : ((height/100)*95)
},
left_bottom : {
x : ((width/100)*5),
y : ((height/100)*80)
},
left_top : {
x : ((width/100)*5),
y : ((height/100)*20)
}
},
curve : {
top : {
x : ((width/100)*50),
y : 0
},
top_right : {
x : ((width/100)*92.5),
y : ((height/100)* 7.5)
},
right : {
x : width,
y : ((height/100)* 50)
},
bottom_right : {
x : ((width/100)*92.5),
y : ((height/100)*92.5)
},
bottom : {
x : ((width/100)*50),
y : height
},
bottom_left : {
x : ((width/100)*7.5),
y : ((height/100)*92.5)
},
left : {
x : 0,
y : ((height/100)*50)
},
top_left : {
x : ((width/100)*7.5),
y : ((height/100)*7.5)
}
}
}
img.onload = function() {
shape.beginPath(shape.node.top_left)
shape.createCurve(shape.curve.top, shape.node.top_right);
shape.createCurve(shape.curve.top_right, shape.node.right_top);
shape.createCurve(shape.curve.right, shape.node.right_bottom);
shape.createCurve(shape.curve.bottom_right, shape.node.bottom_right);
shape.createCurve(shape.curve.bottom, shape.node.bottom_left);
shape.createCurve(shape.curve.bottom_left, shape.node.left_bottom);
shape.createCurve(shape.curve.left, shape.node.left_top);
shape.createCurve(shape.curve.top_left, shape.node.top_left);
image.closePath();
image.clip();
image.drawImage(img, 0, 0, width, height);
image.restore();
}
img.src = "http://goo.gl/pC8iIF";
</script>
Continue reading →