Generic OGNL Controller without Spring WebFlow

While working on various web projects many times I felt guilty for writing controllers that perform one-two rows of the code.
It must be easier way! And when I read and tried Spring WebFlow I really liked their approach for defining how to call methods on any exposed bean.
It is what I was looking for. Simple things made easy inside the xml config:

 <bean-action bean="cartController" method="changeQuantities">

 	<method-arguments><argument expression="flowScope.viewCommand"/></method-arguments>

 	<method-result name="viewCommand" scope="flow"/>

 </bean-action>

However Spring WebFlow has many aspects that would stop you from using it for entire site. Click-around navigation and SWF do not play well together.
So what I need is ability to define OGNL expression in Spring coonfig without using SWF. What looked scarry on first sight turned to be extremely easy task.
I did some prototyping of idea and here is what I got in spring config file:

 <bean id="profileView" class="com.cochlear.mcc.web.GenericFormController">

 	<property name="formView" value="profileView"/>

 	<property name="manager"><ref bean="profileManager"/></property>

 	<property name="viewScript"><value>manager.getProfile()</value></property>

 </bean>

 <bean id="profileForm" class="com.cochlear.mcc.web.GenericFormController">

 	<property name="formView" value="profileForm"/>

 	<property name="manager"><ref bean="profileManager"/></property>

 	<property name="modelClass"><value>com.cochlear.mcc.profile.model.ProfileDTO</value></property>

 	<property name="viewScript"><value>manager.getProfile()</value></property>

 	<property name="postScript"><value>manager.updateProfile(#object)</value></property>

 	<property name="successView" value="redirect:profile.view"/>

 </bean>

Not as clear as in SWF maybe but enough to prove the idea. It is concise, it does the job. You do not have to write FormActions for trivial things.
Here is what I have in GenericFormController:

public class GenericFormController extends SimpleFormController{

 protected Object manager;

 protected String viewScript;

 protected String postScript;

protected Object formBackingObject(HttpServletRequest request) throws ServletException, InstantiationException, IllegalAccessException {

 	try {

     	if(StringUtils.hasText(viewScript)){

 			OgnlContext context= new OgnlContext();

 			populateOgnlContextCommon(context, request);

 			Object expr = Ognl.parseExpression(viewScript);

 			Object res=Ognl.getValue(expr, context, this);

 			return res;

     	}else{

     		return getCommandClass().newInstance();

     	}

 	} catch (OgnlException e) {

 		e.printStackTrace();

 		throw new ServletException(e);

 	}

    }

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,

                                    Object commandObject, BindException errors) throws Exception {

Object command = getCommandClass().cast(commandObject);

 	try {

     	if(StringUtils.hasText(postScript)){

 			OgnlContext context= new OgnlContext();

 			context.put("command", command);

 			populateOgnlContextCommon(context, request);

 			Object expr = Ognl.parseExpression(postScript);

 			Ognl.getValue(expr, context, this);

     	}else{

     		throw new ServletException("No postScript specified");

     	}

 	} catch (Exception e) {

 		errors.reject(null, "Error:"+e.getMessage());

 		return showForm(request, errors, getFormView());

 	}

return new ModelAndView(getSuccessView());

    }

protected void populateOgnlContextCommon(OgnlContext context, HttpServletRequest request){

 	  context.put("request", request);

 	  context.put("session", request.getSession());

 	  context.put("user", SecurityContextHolder.getContext().getAuthentication().getPrincipal());

    }

// Getters and Setters here ..

 // ...

}

When I have time I will do something more production grade. For now I just glad to know that it is achievable and quite simple actualy.

DiggIt | del.icio.us

15 Responses to “Generic OGNL Controller without Spring WebFlow”

  1. Alexey Says:

    The standard argument against moving code to XML is the future manageability (adding some type-unsafe scripting to strict language) and dumbness to automatic refactoring. If you rename or change the signature of the updateProfile() method you’ll have to manually rename/change it in your spring config file. IDEA will probably cope with renaming but not with changing signature.
    Actually I don’t see why writing these several lines in XML config is easier than writing a two-line java class. And what advantages it brings to you other than creating one class less?

  2. Robert Sayfullin Says:

    I absolutely agree with you about the fact that any practice in software development is arguable. I personally was many times disappointed on my memory, Eclipse’s refactoring and Spring WebFlow when changes of method signatures were not reflected in 20K blah-blah-flow.xml.
    But there is other side. If you look at typical small webapp you will see that it looks like onion in part that deals with CRUD and other trivial functionality.
    You wrap getHibernateTemplate in DAO, DAO in manager, manager in service, services are used in controllers. Good practice definitely. However sometimes if we know that all this functionality will be just set of wrappers and you never gonna change ‘getObjectById’ signature we can sacrifice our habits and break some rules. Then and only then such weird stuff as Generic managers or Generic OGNL controllers are allowable.

  3. simaer Says:

  4. dhaval Says:

    respected viewers,
    i wnat to know that can we use more than one command(Entity) class in controller in spring.

  5. hydraulic car jack Says:

    This is the first time I comment here and I should say you give us genuine, and quality information for other bloggers! Good job.
    p.s. You have a very good template . Where did you find it?

  6. Slim@yahoo.com Says:

    umm… I am no

  7. cialis Says:

    You should participate in a contest for the most effective blogs on the web. I’ll advocate this website!

  8. samuel Says:

    HMS0rB http://dy5Ndo9BsL1vpVv5kqsUh.com

  9. Tzwvhpkh Says:

    How long have you lived here? nymphet stories preteen >:-[[[

  10. Rfuqpdid Says:

    What university do you go to? underage softcore pics wwf

  11. Ohfkwtrk Says:

    Do you know the number for ? lolita sex
    140555

  12. Otdugtwq Says:

    Could I borrow your phone, please? nn lolita models
    663480

  13. Urlkfmyk Says:

    Jonny was here Preteen Nude :(((

  14. Jfftyfep Says:

    I’d like to pay this in, please Preteens Preteens Nymphets xhm

  15. Lguovyxt Says:

    I wanted to live abroad http://cuberagytatu.de.tl elwebbs incest Lisa ann is always hot as fuck, but tbh in this movie, that daughter was goddamn sexy, who is she??

Leave a reply to samuel Cancel reply