register
other register

Wednesday, September 16, 2009

Java Regular Expression Experience.

If the input only allows the number format like the following: 1, 11, 1.1, 1.11, 11.1, 11.11.

class Test {
java.math.BigDecimal price

static constraints = {
price(blank: false, validator: {val, obj ->
// this will only allow those 33.33 format
if (!Pattern.matches("\\d{0,2}\\.\\d{0,2}", val.toString())
&& !Pattern.matches("\\d{0,2}", val.toString())) {
return ['digital']
}
}
)
}

Please refer to Java Regular Expression docs for more info.


In addition, make sure the mysql table column price with the right (enough) type: decimal (10, 2).

Tuesday, September 15, 2009

Calendar datepicker Plugin in Grails

Refer to Grails Calendar Plugin site for how to install and use the plugin.

<g:form>
<calendar:datePicker name="myDate" defaultValue="${new Date()}" dateFormat="%d/%m/%Y"/>
<g:actionSubmit name="myAction" value=Submit"/>

There will be some html and javascript code generated by the plugin when viewing page source. The code we care is:

...
<input type="text" id="myDate_value" name="myDate_value" readonly="true"/>
...

The submitted myDate will be in String format dd/mm/YYYY, and we will use myDate_value in our controller to retrieve value.

class MyController {
def myAction = {
if (params['myDate_value'] != null) {
def f = new SimpleDateFormat("dd/MM/yyyy")
def myDate = f.parse(params['myDate_value'])

// Testing
println myDate.class // it is java.util.Date
println myDate // Tue Sep 15 00:00:00 BST 2009

def test = Test.get(1)
println test.dateCreated.class //java.sql.Timestamp
println test.dateCreated // 2009-09-14 10:34:10.0

// Time comparison
println myDate > test.dateCreated // true
println myDate == test.dateCreated // false
println myDate < test.dateCreated // false
....
}
}
}
The unique constraint in Grails works fine out of the box when the field is not a primary key. But it cause exceptions if the field is a manually assigned primary key.
Also refer to Grails doc for unique constraint.

class Test
String id

static mapping = {
table 'test'
id generator:'assigned', column:'id'
}
static constraints = {
id(blank: false, unique: true)
}


Exception will be thrown when saving it with a duplicate id.



The solution is as below:

class TestController {
def save = {
def testInstance = new Test(params)
try {
if(!testInstance.hasErrors() && testInstance.save()) {
flash.message = "Test ${testInstance.id} created"
redirect(action:show,id:courseInstance.id)
}
else {
render(view:'create',model:[testInstance:testInstance])
}
}
catch (org.springframework.orm.hibernate3.HibernateSystemException e){
// Field in view to highlight using tag
testInstance.errors.rejectValue('id', 'test.id.notUnique')
render(view:'create',model:[testInstance:testInstance])
}
}
}

In the message.properties file, add one entry:

test.id.notUnique=The system has already had a id as you entered. Please provide a unique id.

Remove Cookie in Servlet

In Firefox, view the cookie info via Web Developer plugin. Make sure the path is correct, that is where the cookie located.



 


 


In Servlet, to remove a named cookie do:

Cookie myCookie = new Cookie("JSESSIONID", "");
myCookie.setMaxAge(0);
myCookie.setPath("/");
//myCookie.setPath("/" + grailsApplication.metadata['app.name']);
response.addCookie(myCookie);

Refer to the Java Servlet Cookie doc, set the MaxAge to 0 will delete the cookie.