18 March 2010

Nested HTML FORMs

It is possible to have multiple forms within the same Html document but it is NOT possible to have a Html Form nested within another Form. See the W3C Html Form for reference. This is a fact that has escaped my memory once or twice throughout my time been a web developer so to avoid wasting time it's important to remember this basic rule.

Is there a way around this? The short answer is yes using javascript and html which isn't really a nested form but behaves like one. This jQuery nested form solution is a pretty neat way of doing it.

12 March 2010

Using Stripes custom checkbox jsp tag

Stripes is an excellent, lightweight Java web MVC which just works with minimum effort... well most of the time. I got stuck recently when trying to use the stripes checkbox tag and I found the stripes checkbox javacdoc slightly ambiguous. This is how it's done...

The Stripes checkbox name value should map to an attribute on the view's corrosponding ActionBean. Ensure this attribute follows the standard JavaBeans specification (getters/setters) so Stripes can use it. All standard stuff so far, it's the next bit which confused me. Within your Jsp checkbox tag the checked value must MATCH the ActionBeans at tribute's value which is specified in the name value. Here's an example:



public MyActionBean extends ActionBean{

private boolean checkedValue;

public Resolution view(){
checkedValue = true
return new ForwardResolution(/myactionbean/view.jsp);
}

}



3 March 2010

The difference bewteen overloading and overriding in java: SCJP 6 exam revision

A key concept in object orientation and the java language. You should know this very basic stuff if you're a Java Developer. Here's a summary for newbie's:

Overriding only applies to inhertited methods when a subclass re-implements a superclass's method. An overriding method:

- MUST have the same parameters (arguments) as the method it is overriding
- MUST have the same return type as the method it is overriding, except for the case of co-varinat return in java 5 (class in same heirachy is allowed)
- MUST NOT restrict the access modifier i.e. public can not become private but private can become public
- MUST NOT throw new or checked broader exceptions
- CAN throw fewer or narrower exceptions or any unchecked exception

Overloading refers to methods that share the same name but different parameters. Nothing to do with inherentience. Overloaded methods:

- MUST have different parameters
- CAN have a different return type
- CAN have a different access modifier
- CAN throw different exceptions

Java Regular Expression Searching for Expression Language Script within a String

I recently needed to search for an Expression Language (el) script within a String e.g. "hello my name is people[${loop.index}].name". I decided to use RegEx as it's the most efficient solution. The following code snippet searches for multiple occurrences of el script blocks within a String and add's them to a List:


String haystack = "hello my name is people[${loop.index}].name";

Pattern pattern = Pattern.compile("\\$\\{\\w+(\\.\\w+)*\\}");

Matcher matcher = pattern.matcher(haystack);

List matches = new ArrayList();

while(matcher.find()){
matches.add(matcher.group());
}