Friday, March 21, 2008

Quick Sequence diagrams

Though there are great tools available to draw UML diagrams , some times we will be in a situation to quickly draw a sequence diagram for a presentation or to discuss some thing in a design meeting and we dont want to waste our time working with those heavy tools.

I came across http://www.websequencediagrams.com/ and felt this a cool site in which we can describe our sequence diagrams using simple text like A->B:create() to produce cool diagrams like the below image.

how to access constants from JSTL (EL)

During our web development we may come across a situation where we want to use a constant (static final) variable for some reason in our JSP using JSTL's Expression Language (EL). As you know we access properties very easily using JSTL as below.

you might alreay know.. if we want to access a property called studentName and a method is defined as getStudentName() in the target object (lets cal it 'student') the EL would be "${student.studentName}".

But if we want to access a constant defined in the above class as
public static int MAX_RETRY_ATTEMPTS=10;

using the EL "${student.MAX_RETRY_ATTEMPTS}" , will fail
I am sure you will need to use these constants sometimes.

We can use a simple trick to solve this.

we will make use of a Map to store the constants , as below


.....
.....
......
public final static int MAX_ALLOWED_USERS=10;
public final static int MAX_LOGIN_ATTEMPTS=2;
Map constants=new HashMap();

public Map getConstants(){
constants.put("MAX_ALLOWED_USERS", MAX_ALLOWED_USERS);
constants.put("MAX_LOGIN_ATTEMPTS", MAX_LOGIN_ATTEMPTS);
return constants;
}


As you can see we have defined a method getConstants which returns a HashMap with our constants with appropriate keys.

Now how can we use this in our JSP ?


The Maximum number of attempts is : "${MyForm.constants['MAX_LOGIN_ATTEMPTS']}"


clearly, we first get the Map using getConstants() method and then value of the constant using the key 'MAX_LOGIN_ATTEMPTS'.

Easy ?

Please let me know if there is a better way to do this

Wednesday, March 19, 2008

What's nice about seaside ?

Its been very interesting week, trying to get my head around seaside. Honestly it took some time for me to unlearn many things which I learned over past many years. Not only a great idea, I feel it will speed up the the web development process in a great way. Although many seaside experts have written great examples, I wanted to post the one I started with.

The functionality is quite simple
1) Display the Login Page
2) User enters username and password and clicks on validate button
3) If the credentials are validated (in this case username='smalltalk' ) a welcome message is displayed , otherwise login page is displayed again.

Quite simple.... lets see how intuitive it can be

Lets start with defining our Login Component as below



WAComponent subclass: #Login

instanceVariableNames: 'username password retry'
classVariableNames: ''
poolDictionaries: ''
category: 'LearnSeaside'!



this should be self explaining , We defined a class Login subclassing it from WAComponent.
It has three instance variables , I used "retry" to indicate if it is a retry or first attempt. (may not be the best way ..)

Seaside calls renderComponentOn method when it is appropriate to display the content on the web page. Let us look at how we want to display the content on page.


renderContentOn: html

html
form: [
retry ifTrue: [
html text: 'Retry..'.

html break].
html text: 'User Name'.
html textInput on: #username of: self.
html passwordInput on: #password of: self.

html submitButton callback: [self answer: self];
value: 'Validate']



Nothing special .. it just displays user name and password fields, in addition to that , it display text "Retry" based on the instance variable "retry".

Now lets add some magic. we will now write a Task to start this.

we will name our task WASchoolTask , subclassing WATask. it will have a method "go" which will be called by seaside as entry point to the application.

let us look at the go method.


go
| login |
login := self call: Login new.
[login username = 'smalltalk']
whileFalse: [login retry: true.
login := self call: login].
self inform: 'Welcome ' , login username

as you can see in the above code we want to display the login screen until the user enters username as smalltalk. What a great way to write your applications describing the natural flow of the application! Notice the statement
self inform:'Welcome ',login username

The best part of the code is that this statement will not be executed unless the loop is broken. Imagine how you would do this in a Action based framework.

Sunday, March 2, 2008

Some new ways to Develop web applications

Its been long since I started working on developing Java based web applications. Quite frankly, developing applications, although critical for earning rice :-) , I am no more enjoying developing them with the MVC models using heavy boilerplate code and scaringly big XML files for configuration.
The typical request response model sounded great for me in the initial days , probably I was used to develop using cgi and later php with code scattered all over the page.
The interest in some different stuff automatically dragged me to Ruby on Rails, and found it really cool, but again the basic approach was same but with emphasis on convention over configuration.

Looks like atlast I found some thing interesting in Seaside , a Continuation based framework in Smallatalk !! Yess.. I wrote it correctly , It is Smalltalk, that language we heard about in our College days.


So whats new about it ? Lets have a look at the following figure



In a typical Struts based or any Action based framework , Control flows from Browser to Action Class (or similar ) for processing some business logic by parsing the request parameters (many framework do this automatically ..but still we are dealing with fields and request parameters) and based upon the result control will be passed to render appropriate page, and this process repeats. This model allows business logic to be written in separate classes .

Now , lets us look at the Continuations based Seaside framework, In a striking contrast with the above approach , we can write our web applications , very similar to the way we write any console based application, like the below pseudo code

1) Get Values From Browser
.... Suspend Processing till user click on Submit
2) Process
3) Display Result
..Suspend processing till user responds with something
4 ....


I found it very different and very intuitive way of developing applications. There is a lot of Buzz around this framework , you too may find it interesting.

Currently I am exploring this framework and finding it a great way to develop the applications. I will write about this further in my future posts.

Saturday, March 1, 2008

Handling multiple checkboxes in Struts 2

Many a time in Web applications we implement a form with a table of records with checkboxes , allowing users to select some rows for performing operations on selected rows. In this post let us look at how it is implemented.

This example is based on Struts 2 framework, with StudentAction as the action class and student.jsp as JSP to display a list of students

The general requirement in these forms is to be able to get the list of selected rows in the "Action" class to perform the required operations and when the control is passed to render the jsp the previously selected rows should be still shown as selected. To achieve this we will add a special field to the StudentAction to hold the list of selected rows (normally identified with some primary key) , as shown in the below code snippet from StudentAction.java


public class StudentAction extends ActionSupport {
List students = new ArrayList();
List selected = new ArrayList();
public List getSelected() {
return selected;
}
public void setSelected(List selected) {

this.selected = selected;
}


.....



The corresponding JSP page to display the list of students with a checkbox will have to name that checkbox as "selected" as shown in below snippet

 















What this code does ? look at the line





if you find everything is straight forward except value="%{name in selected}" , it is a OGNL expression which returns "true" if the value returned by getName() (on the StudentAction instance) is contained in the list returned by getSelected() method, and because this list will be empty for the first time no checkbox will be selected (as the above expression returns false), on clickin Submit button after selecting some checkboxes Struts2 will automagically populates the selected List with the values of selected checkboxes (which happens to be name here), after the performing the business logic when the page is displayed again the above expresion will return true for the previously selected names resulting in those checkbox rendered selected.

Easy ?