My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Tutorial  

Featured, Phase-Deploy
Updated Jun 1, 2009 by daniel.kvasnicka.jr

This tutorial explains how to create a simple Spring+Flower-powered web app

  1. The basic structure | Create a basic Java web app structure using your favorite tool, e.g. new Dynamic Web Project in Eclipse.
  1. Setup libraries | If you check out the Flower source code and run the Ant build process (described in README), you get a set of JARs in WebContent/WEB-INF/lib. Take those and add them to your own web app lib direcrory. It should be the following libs:
  • commons-beanutils-1.7.0.jar
  • commons-codec-1.2.jar
  • commons-collections-3.2.jar
  • commons-fileupload-1.2.jar
  • commons-httpclient-3.1.jar
  • commons-io-1.4.jar
  • commons-logging-1.1.1.jar
  • log4j-1.2.15.jar
  • spring-agent-2.5.6.jar
  • spring-aop-2.5.6.jar
  • spring-beans-2.5.6.jar
  • spring-context-2.5.6.jar
  • spring-core-2.5.6.jar
  • spring-test-2.5.6.jar
  • spring-web-2.5.6.jar

Note: Not all the listed libs are probably needed, but this is how they are downloaded from the Maven repository (and if you look in the Ivy config file, you'll notice that there are already tons of useless libs excluded -- if you need them, delete the exclude tag)

  1. Add Flower JAR | Then add the spring-flower-*.jar downloaded from here.
  1. Setup web.xml | Setup Spring in your web.xml (the ContextLoaderListener stuff, you know...) and add the following:
<servlet>
        <servlet-name>flowerServlet</servlet-name>
	<servlet-class>net.danielkvasnicka.flower.FlowerDispatcherServlet</servlet-class>
</servlet>
	
<servlet-mapping>
	<servlet-name>flowerServlet</servlet-name>
	<url-pattern>/the/url/pattern/you/want/flower/to/be/sensitive/to</url-pattern>
</servlet-mapping>
  1. Setup your Spring application context | Add the following to your Spring app context:
<!-- FLOWER config -->
<import resource="classpath:net/danielkvasnicka/flower/core/flowerContext.xml"/>	
<bean id="webAccessibleBeanResolver" class="net.danielkvasnicka.flower.core.DefaultWebAccessibleBeanResolver" autowire-candidate="true" />
<!-- FLOWER config -->
  1. Create your web accessible beans | Now the only thing you have to do is create your beans (you will probaly want to extend net.danielkvasnicka.flower.beans.AbstractWebAccessibleBean -- remember: you HAVE TO implement WebAccessibleBean in your beans) and put them into the Spring ctx, like this:
<bean id="firstBean" class="net.danielkvasnicka.flower.test.beans.TestWebAccessibleBean" f:url-mapping="/helloworld/({f_method}[a-zA-Z]+)/({x}\d*)" />

Don't forget registering the namespace for "f". Add xmlns:f="http://www.danielkvasnicka.net/ns/flower/spring" to the root tag and http://www.danielkvasnicka.net/ns/flower/spring http://ns.danielkvasnicka.net/flower/spring-flower.xsd to its xsi:schemaLocation attribute.

This is taken from the test context available in the Flower sources and it says: when the client requests an URL that conforms to /helloworld/({f_method}[a-zA-Z]+)/({x}\d*) then take this bean and call method, whose name is stored in the named group "f_method" (all named groups staring with f_ are reserved to Flower internals).

So when you call /helloworld/index/3, the bean is retrieved from the context, its parameters property is populated with a map containing {x=3} and the method index() is called. The returned Response instance is then sent (by calling send()) to the client.

For more info on how to create the beans, consult the test sources: http://code.google.com/p/spring-flower/source/browse/trunk/#trunk/test/net/danielkvasnicka/flower/test/beans

Powered by Google Project Hosting