Creating a Struts-Dojo Project in IBM RSA 7.0

October 1, 2008

Hope someday I could publish my experiments with Dojo .And this is the day.

Using IBM RSA 7.0

1.Create  a new dynamic web project with Struts support

2.Add dojo toolkit to project

Dojo toolkit can be obtained from:http://download.dojotoolkit.org/release-1.1.1/dojo-release-1.1.1.tar.gz

Details about Adding explained using screenshot

That means its included in the Webcontent. You can see tons of warning messages caused by silly test html pages inside the toolkit folder,just search around and delete the stuff. Basically it has nothing to do with a struts controller or so.But I just tried using jsp pages,instead of html just to check if we can include toolkit with no problems in the jsp page.

Things to notice.

.Struts link the property of element in the form with form bean.

Like:

<html:text property=”beanprop”></html:text>

Here beanprop is one property in the  form bean.

Here after succesfully deploying the code will be like

<input type=”text” name=”beanprop” size=”20″>    in the html file we are viewing.

Now getting into details:

My JSP page:index.jsp  >> Index.jsp

Here instead of putting property attribute in the text field element ,i have used name because if you are using property here in this jsp page ,your dojo parser wont recognise the tag and the purpose for introducing dojo is defeated.

So our aim is to:

1.retain the dojo functionality

2.Obtain the value from page to formBean

My widget is specified like:

<input type=”text” id=”use” name=”username” size=”30″
dojoType=”dijit.form.ValidationTextBox”
required=”true”
promptMessage=”Enter username”
invalidMessage=”Username is required.”
onchange=”user()”
/>

Here look at the name attribute its username,which is same as my bean property :) and your problem  is solved.The tag itself is self explainatory:

dojoType=”dijit.form.ValidationTextBox” <<Causes dojo parser to recognise it as a ValidationTextbox having validation features included to normal textbox

required=”true” specifies whether field is required( Hurray!U guessed;It helps the parser to recognise whether validation is enabled).(But that alone is not sufficient(We have to specify a regExpression or rule to accept the textbox value)

promptMessage=”Enter username” is the prompting message on clicking the form element it will be visible

invalidMessage=”user()” causes a javascript function user() to be called when text changes in text field.

So inorder to make this all happen,we have to include

<script type=”text/javascript”
src=”dojo-release-1.1.1/dojo/dojo.js”
djConfig=”parseOnLoad: true”></script>
<script type=”text/javascript”>
dojo.require(”dojo.parser”);
dojo.require(”dijit.form.ValidationTextBox”);
</script>

inside your header

Tells dojoParser to load on document load and  parse the html before display

and dojo.require(”dijit.form.ValidationTextBox”) tells parser that  dijit.form.ValidationTextBox is used in the page here,index.html

Output screen

Look at the prompt

Look at the prompt

Now if you have skipped the field this will be the result (beware)

This is the error prompt

This is the error prompt


SubClassing in Struts

October 1, 2008

This tutorial discusses about the need of  subclassing in Struts Action class.

Have you ever been in trouble decribing tons of action classes to do  jobs which are related having same data to handle(a single formBean).And here is your answer.

Consider a usual scenario

AddEditDelete User

Add Edit Delete User

Here you know that one action class can refer to only one formBean and you are using three action classes to define three functions add user,delete user,edit user which uses the same formBean

Now consider a restructuring in this way which you actually need

Here You are using Action subclassing to achieve this format.

Now the changes in the action class.

1.Instead of extending action i am extending DispatchAction here.

And so there will be no execute method ,instead we can specify our own method name but the signature should be intact as that of an execute function

public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { }

public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { }

public ActionForward editUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { }

These three functions do the specified jobs ,implied by their names.

For doing this subclassing you need to edit the struts-config file so that it can recognise which function to take based on the submit from a jsp page.

Inorder to do that we specify parameter attribute in our action class definition in the struts-config.

<action name=”formBean” path=”/path” scope=”session” type=”dojeg.actions.Action” parameter=”func”>

here my parameter value is func,now you are ready to do the subclassing.

But how to do it.

The Add User is called when the url is like

/path.do?func=addUser

The Edit User is called when the url is like

/path.do?func=editUser

The Delete  User is called when the url is like

/path.do?func=deleteUser

Hope you have understood this method

Now how to access these from  jsp page.

Simple: <html:submit  property=”func” value=”addUser”></html:submit>

and form  tag as:html:form action=”/path”> where path is my action class name

Here when user clicks the Submit button addUser function is evoked

Thats the way to do it.
We can use LookUpDispatchAction,MappingDispatchAction(Struts 1.2) to do the same.

LookupDispacthAction helps to specify the names separate from being hardcoded into jsp page.


Ajax with Struts the Dojo Way

October 1, 2008

Tutorial Requirements

My Index.jsp Index.jsp

My action class action class

Here I am using my jsp from previous post.Hopefully you people have noticed the user() function and there lies the call to ajax from the widget.It just calls an action class function.

Here with no prompting,I have used the DispatchAction method to execute my ajax call(That doesnt matter here).I am concentrating on the ajax side.

My script containing function user is:

<script type=”text/javascript”>
function user(){
dojo.xhrPost( {
url: “/path.do?func=ajax”,
handleAs: “text”,
load: function(response) {
alert(response);
}
}
);}
</script>

here I will explain from

dojo.xhrPost which itself is a function

It submits a HTTP POST request asynchronously. Use xhrPost when you want to send form data to a website and the form doesn’t contain any file-input fields. (use dojo.io.iframe.send() instead)

[taken from:http://www.dojoforum.com/2007/10/11/dojo-example-xhrget-and-xhrpost]

Its arguments

url:to which url the request is forwarded:

In this case path.do is my action class ======> /path

handleAs:text tells the response to be handled as a text

load:function(response) causes the function to be loaded directly on returning successfully from the servlet(our controller class and everything works fine).

Here i havent included any arguments in my url except the hardcoded func=ajax

(We can do so) specifying parameters as the second argument.

which selects the function to be called in the action class,wait I will explain about my action class shortly.

My action class

public ActionForward ajax(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
FormBean formBean = (FormBean) form;

try {
PrintWriter p=  response.getWriter();
p.println(”Hi im using ajax”);

} catch (Exception e) {

errors.add(”name”, new ActionError(”id”));

}

if (!errors.isEmpty()) {
saveErrors(request, errors);
}
forward = mapping.findForward(”success”);

return (null);

}

Now you have noticed we have returned null instead of the forward string,that does the trick.

PrintWriter p=  response.getWriter();
p.println(”Hi im using ajax”); is used to output response .

Hope you have got a clear picture.

And now the result page is: