Calendar
QuicksearchBookmarksMy del.icio.us
ArchivesCategoriesSyndicate This BlogBlog Administration |
Thursday, August 31. 2006Creating a maven archetype for acegi and springframework
I have created a lot of small applications that include spring and acegi. Most of the time I copy most of the resources from another project. Some time ago I learned about the maven archetypes. An easy way to create the project structure. That was the time I decided that I wanted to have my acegi springframework archetype. Last weeks I found some time to create this archetype. You can download the files here:
http://code.google.com/p/gridshore/ And the generated maven site here: http://www.gridshore.nl/projects/springframeworkarchetype/index.html Ofcourse there is also a maven website about creating an archetype, this is not a very extensive website. In this blog item I will explain more about the archetype I created. First thing to know, well surprise, the archetype is just a maven project. You can start you new archetype by using a maven archetype: mvn archetype:create -DgroupId=nl.gridshore.samples.archetype -DartifactId=SpringframeworkArchetype -DarchetypeArtifactId=maven-archetype-quickstart Have a look at the pom for the other properties; scm, distributionManagement, etc. The project itself contains an src\main\resources\META-INF\maven\archetype.xml file that defines all the resources that need to be copied.
The structure is pretty easy, you can find all the resources under the directory: src\main\resources\archetype-resources You can use the following parameters in your file as placeholders: http://maven.apache.org/plugins/maven-archetype-plugin/create-mojo.html. Have a look at the src\main\resources\archetype-resources\pom.xml for an example:
Hope that helps you on your way to your own archetype. If you just want to use mine:
Friday, August 25. 2006Profict Java Summer Camp
Yesterday I attended the Profict Java Summer Camp. And what a camp it was. The first surprise was the location, what a nice environment to do your work. In introduction of profict, "we are hiring". Then the real camp started. 4 hours head on with Greg Murray. Greg is working for more then 10 years with java, mostly for sun. The last years he has been busy with AJAX. He gave a very enthousiastic talk about a ajax and some frameworks. Just to give you some pointers:
Dojo framework, jMaki frameowork, ajax blueprints, netbeans. He showed a lot of examples, a few I liked very much. http://www.javaserver.org/jmaki/ https://blueprints.dev.java.net/ajax.html http://www.isoma.net/games/goggles.html (Oops not ajax) In Brief, a very nice presentation with a lot of information. It could have been a little bit shorter. Some nice pointers to start new research into java The next presentation was by Bram Smeets of interface21. We allready know him, read one of the other posts about dwr on my blog. He gave a very good introduction to the powers of dwr and what it can do if you combine it with spring. I guess I allready knew, still, it was interesting to see and hear Bram talk about it and put some stuff into perspective. I am curious about the next step of dwr and the use of spring 2 stuff within DWR. After the presentations there was a nice barbeque, had some good conversations with Greg, Bram and others. It was a very nice camp, thank you profict. Tuesday, August 15. 2006Google code
Last week I started experimenting with google code. What a simple thing it is. Nice to have a svn repository and have some very small project toolset as well. There is an issue list, you can add developers, have some mailings. No to much, but it is working. From now on you can find my sample code in this location:
http://code.google.com/p/gridshore/ Have a look, and if you find issues, please report them via the tool. Wednesday, August 2. 2006Creating an autocomplete with Spring and DWR
There is a lot of talk around the web about autocomplete with ajax. I have blogged about a solution with ajaxtags and spring before (link). It is a nice solutions that works pretty well. Still I did not feel right to have to do a lot of configuration within spring and do something special in your normal code. I also had a look at dwr. This is a nice library that lets you expose a serverside service to your javascript. A number of javascript libraries are generated runtime, you can use these libraries from your scripts within the browser. There is a nice integration with script.aculo.us. Now you can use a lot of components out of the box. Next step, use the script.aculo.us autocomplete component with dwr. Well that is easy said. There are some blog items on the web that tell you how to do this. The best way is to have a look at some sample code Bram smeets made on his blog. I used his code to create a sample that uses maven to build and package. I also used the callisto ide, works very good as well. I would not try the maven plugin yet, I did but it fails for now.
After this rather long introduction, time to get the coding done. Step1 Get your environment in place. Run the maven archetype command to create a webproject template. Alter some of the default values to make it your project and put in dependencies on the following libraries to your pom.xml file: spring-webmvc-1.2.7 (org.springframework) dwr-1.1.1 (uk.ltd.getahead) Execute the command to setup your eclipse environment and startup eclipse. commands:
Step 2 Time to explain a little bid what the application is going to do. Actually there is two phases in the application. First search for a known postalcode. If you have found the right one and pushed the search button, the accompanying address is obtained. Sounds easy doesn't it? We are going to expose a service object with two methods:
It should be obvious what the implementation of this interface should do. To be able to do an easy test, I created a mock implementation for this interface. The mock contains a hashtable with some postal codes as a key and the belonging address as the value. The two methods mentioned before use this hashmap to return all postalcodes or the address for a specific postal code.
Fianlly there is a transferobject called AddressTO that contains the following 4 fields: streetname, houseNumber, city, postalCode. Download the source to have a look. We have created the interface and an implementation. Time to do some spring configuration. We only need to create one bean, therefore the config file is very easy
Step 3 After this everything is in place to configure dwr to expose the spring bean. To be able to use dwr, we first need to add the dwr servlet to the web.xml file and load the spring context:
Dwr uses a configuration file to configure the beans that are exposed to your javascripts. This file is next to web.xml and is called dwr.xml.
By default you allow all methods to be called, you should allow only the real ajax usable methods. The create element is like the constructor, I tell dwr to use spring to create the javascript object AddressServiceFacade based on the spring bean addressServiceFacade. Because a use a transfer object for the address I also need the converter. Everything is ready to do the first test. Deploy the application to tomcat and call the following url: http://localhost:8080/ajaxdwrsample/drw You should get a page with a link to AddressServiceFacade, click this link and have a look at the next page: ![]() test page of dwr On this screen you can test your dwr ajax service. Use the execute buttons and input boxes to test the services (see the red box). Great isn't it? Step 4 And now for the webpages. First add the script libraries to your war file. I added the Prototype script and the script.aculo.us. to the folder "scripts". Next to that I added to style sheets (I copied all these files from the sample by Bram Smeets). Out of the box there is no support in script.aculo.us for autocomplete via dwr. Therefore I used the excellent component from Bram to implement it. The component resides in the "autocomplete.js" file. Finally the index.jsp that implements the real automcomplete component. The includes:
Important in the includes is the AddressServiceFacade, this is the generated script file by dwr based on the dwr.xml file. There we configured this name ... javascript="AddressServiceFacade" .... Now the standard scripts are in place, do the html thing:
The constructor for the autocomplete component has 4 parameters:
The callback function looks like this:
Doesn't look very difficult, does it? Beware of the function nameValueSelector, it receives a tag object. This can be a simple object (like in my case, a string). It can also be a javabean representation, then you need an extra method to obtain the property to show in the autocomplete box. If you return a list with AddressTO objects and want to show the postalCode of the address, this line would be : return tag.postalCode. The autocomplete box will now look like the following image: ![]() Your autocomplete is done, there is one extra thing in the sample. You can select the postal code and look for the belonging address. That is done the with the following piece of html and javascript.
The id's of the span elements resemble the fields of the AddressTO javabean. With the help of the dwr utility package we can easily fill these spans with data obtain via dwr. This is done with the following to functions.
The handleAddClick button receives the onClick event from the search button. First we obtain the value from the input field, then we call the dwr javascript object. This method obtains 2 parameters, one more than the real method. The extra parameter is the callback function. This function gets called with the return value of the original java function. We configured the fillAddress function to be the callback function. The special util function "DWRUtil.setValues" sets the values of the address to the spans. The end result looks like this: ![]() Download the sources here : ajaxdwrsample.zip That's it, hope you like this article. Thanks again to Bram Smeets for his autocomplete component and of course the excellent dwr and script.aculo.us frameworks.
(Page 1 of 1, totaling 4 entries)
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||

