Stephen on Software aka SOS

March 31, 2009

Ext JS – Disabling FormPanel submit if validation fails

Filed under: extjs — sljm @ 3:17 pm

To disable the FormPanel’s submit button if the validation fails (example like allowBlank:false). You will need to set the FormPanel’s monitorValid to true and the submit button’s formBind to true.

var form=new Ext.form.FormPanel{
    monitorValid:true,
    items:[{
      name:"testing",
      xtype:'textfield',
      fieldLabel:'Test',
      allowBlank:false,   //Must be filled in
      value:''            //Set the value so that the validation will trigger
    }],
    buttons:[{
        text:'Submit',
        formBind:true   //If validation fails disable the button
      }
    ]
}

In some cases you might need to set the value of the textfield or combobox explicitly to empty string. If not the validation will not kick in.
Links

Login Form: Disable ’submit’ until username has been input

March 26, 2009

Visual VM –

Filed under: Uncategorized — Tags: — sljm @ 2:17 pm

Found an interesting video on how to use Visual VM that comes with JDK 6 update 7 to see what’s happening in Glassfish. Looks like an interesting tool to find out whether your app servers are leaking memory.

Java VisualVM User Guide:
http://java.sun.com/javase/6/docs/technotes/guides/visualvm/index.html
VisualVM project page:
https://visualvm.dev.java.net/gettingstarted.html

March 4, 2009

Spring DataBinding and PropertyEditors

Filed under: Uncategorized — Tags: — sljm @ 9:53 pm

To bind data from a <form> to a SimpleFormController is easy, just read Spring MVC Step by Step in the Spring distribution and they show you how to do it. But what happens when you need to translate strings that represent dates to Date objects? or translate from JSON string to an Object?

To that you’ll need to create a custom PropertyEditor and override initBinder in SimpleFormController, to tell the controller how to handle the data that return from a HttpServletRequest object. By default it does a simple mapping, that is to say it tries to read the keys from HttpServletRequest and tries to find the corresponding setter in the commandClass and sets the value. If it can’t it will generate a DataBinding error.

e.g

If the class has a setter named setText and the HttpSerlvetRequest has a parameter with the key “text”, the binder will try to set the value of the key into the object specified by the commandClass.

Sample code from overiding initBinder using CustomDateEditor to read dates

protected void initBinder(HttpServletRequest request,
                          ServletRequestDataBinder binder)throws Exception{
SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");
CustomDateEditor cde=new CustomDateEditor(df,true);
binder.setCustomEditor(Date.class,cde);
super.initBinder(request,binder);
}

I suggest you look at this presentation to get a feel of the workflow for the SimpleFormController. For Spring MVC it is important to know how the flow if the controller is so that you can understand how the various overrides will work.

For custom PropertyEditors you can look at the source for CustomDateEditor to get a feel how to implement. Most of the property editors will inherit from PropertyEditorSupport rather then implementing PropertyEditor staright.

It seems like the few methods that you will need to override are the setAsText and getAsText methods. And in these 2 methods you will need to use setValue and getValue to set the correct object so that the binder can retrive the final object. So a custom PropertyEditor might look something like this:

public class CustomEditor extends PropertyEditorSupport{
public CustomEditor(){}
public void setAsText(String text){
//Do some proccessing then call setValue(); to set the object
setValue(someObject);
}
public String getAsText(){
SomeObject obj=getValue();
//Do something to get the thing as text again
}
}

The only puzzling thing (at least to me) is why does Spring use the ProperyEditor interface for something that could probably be implemented by using a simpler interface consiting of the above 4 methods (setAsText,getAsText,setValue,getValue)? I have no idea it seems that lots of methods in the interface are not  being use and they could probably be served by using a simpler interface.

Links

CustomDateEditor source

Blog at WordPress.com.