Monday, 23 February 2009

Spring Web Flow and Spring MVC in one application

Share
Spring Web Flow is ideal for advanced complex page navigation and sophisticated flow controllers, but it is of no use when it comes to free browsing.

Need Spring Web Flow revision? Read my Building Spring Web Flow 2 applications with Eclipse and Maven2 post.

A perfect example of free browsing is YouTube.com. You can click in every link on the screen, copy the URL and send it to your friends.

In Spring Web Flow it cannot be done, the unique execution key passed in URL changes every page load, and the same page can have many different execution keys.

If you want your web application to be more user friendly, more restful then you have to switch to Spring MVC.

There is nothing that prevents you from using Spring Web Flow for account management and registration sub-systems, and Spring MVC for the social or public part of the same application.

Let's see how to do this!

Spring Web Flow and Spring MVC in one web app

Because I haven't shown you yet how to create basic Spring MVC web application, here are some basic steps showing how to do this (I assume you have some basic knowledge of Spring).

You can extend the example given in Building Spring Web Flows 2 applications with Eclipse and Maven2 post. It is a simple Spring Web Flow 2 application to which I will add Spring MVC support.

First add a new Spring dispatcher in web.xml, call it browsing:
<servlet>
<servlet-name>browsing</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>browsing</servlet-name>
<url-pattern>/browsing/*</url-pattern>
</servlet-mapping>
Then, create browsing-servlet.xml file (also in WEB-INF directory) and copy and paste:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.5.xsd">
<context:component-scan base-package="org.xh.studies.projectx.controllers"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass="org.springframework.faces.mvc.JsfView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp" />
</beans>
Spring will automatically scan the classpath (the org.xh.studies.projectx.controllers package and all its sub-packages) for components marked with @Controller annotation.

So, a simple controller like this one:
@Controller
public class HomeController {
@RequestMapping("/home.html")
public String displayHome(Model model) {
return "home/index";
}
}
will be automatically picked up by Spring during the application start up.

The URL of such component consists of (apart of obvious protocol, host, and port parts):
  • application's context path: projectx
  • Spring's dispatcher mapping: browsing
  • request mapping defined in controller: home.html
In our example it is:
http://localhost:8080/projectx/browsing/home.html
Summary

That's all!

Spring Web Flow and Spring MVC can co-exist in one web application without any issues.

Spring Web Flow is indispensable when it comes to creating sophisticated online wizards.

On the other hand, Spring MVC is an ideal choice for social network sites and URL copy and paste friendly systems.

Make up your mind before implementing your web applications or you can end up with re-writing some of the parts of your application week before the deadline :)

Cheers,
Łukasz

2 comments:

Pramatr said...

I'm not quite sure where the vs title comes into this. As you said SpringMVC and WebFlow can live together quite happily. You simply have to use the right tool for the right job. If you need to model a complex flow, use WebFlow.

Jayz said...

if you want to use the spring jsf support SpringFaces, do u think it is still valid to have them work together