Nate's Programming Blog

I finally sat down and worked through setting up acegi security and using spring mvc. I have to say I’m pretty impressed. It really wasn’t all that difficult to setup. It took a little work, but I think I have a pretty good grasp of what’s what. I used the authentication DAO to setup security using a Postgres backend database. The DBC Project I worked on the end of last year and beginning of this year used acegi, but Ryan did all the acegi stuff, plus it was version 0.6 and the version I worked with was 0.8.3 and there are a lot of differences between the two. Also, spring mvc is awesome. It just seems so clean, plus if the rest of your application uses spring, it makes it extremely simple to integrate you web framework with your services or daos. I’ve worked with struts quite a bit and spring seems to solve a lot of the problems I had with struts. Here is a simple example of how you setup a mult-action controller in a spring context file:

<bean 
    id=propsResolver 
    class=org.springframework.web.servlet.mvc.multiaction.
      PropertiesMethodNameResolver >
  <property name=mappings >
    <props>
      <prop key=/processnewuser.htm >addUser</prop>
    </props>
  </property>
</bean>

<bean 
    id=paramMultiController  
    class=org.springframework.web.servlet.mvc.multiaction.
      MultiActionController >
  <property name=methodNameResolver >
    <ref bean=propsResolver />
  </property>
  <property name=delegate >
    <ref bean=userController />
  </property>
</bean>
	
<bean 
    id=userController  
    class=com.natesprogramming.web.controller.UserController >
  <property name=viewName ><value>main</value></property>
  <constructor-arg><ref bean=userProfileService  /></constructor-arg>
</bean>

Basically the userController is the actual controller class. It doesn’t have to implement or extend any base class or interface, which is really nice. Whatever method is to be called just needs to return a ModelAndView object. In this example I’m injecting the userProfileService via the constructor. The paramMultiController’s job is to delegate requests to the appropriate method in the appropriate controller. It is provided a propsResolver who actually maps the specific urls to method names in the controller. Now all you have to do is create some POJO and create the methods with those method names, have it return a ModelAndView and you’re done. It’s that simple!

Anyway, I’m liking it. I do want to try out Tapestry in the near future. It takes a component based approach and is one of the more popular frameworks. Plus I hear Tapestry 4 is up to a beta 2, and that version seems to resolve a lot of the issues with Tapestry 3 like having to have 3 files for every page.


This entry was posted on Wednesday, August 17th, 2005 at 10:52 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
2 Comments so far

  1. Kate on November 18, 2005 11:05 am

    Hi! Can you help me?

    What does ‘userProfileService’ do? Could you show part of spring file where this bean is described?

    Is tag required?

    I have this in my spring file but it does not work 🙁

    aboutPage

    public class CommonDelegate {
    public ModelAndView aboutPage(HttpServletRequest req, HttpServletResponse resp) {
    HashMap map = new HashMap();
    map.put(“pagetitle”, “About”);
    map.put(“header”, “About”);
    map.put(“body”, “About”);
    return new ModelAndView(“common”, map);
    }
    }

    With this code I have only 404 🙁
    HTTP ERROR: 404
    Not Found
    RequestURI=/about.html

  2. Nate on January 17, 2006 10:30 pm

    Sorry for the late response. ‘userProfileService’ is a service object I wrote that the user controller uses. It does not have anything to do with Acegi or Spring MVC, just merely service object that I needed wired in for my example.

    About the example you describe, I see the controller class, but I do not see your spring config entry. You should have an entry in your spring context file similar to my ‘userController’ entry above.

    Also, the example I provided shows one of the ways in which you can map a URI to a method on a controller. This example would be similar to a MappingDispatchAction in struts. I define that the URI ‘/processnewuser.htm’ should map to the addUser method on the UserController object.

    Please let me know if you need anymore help or if I can explain anything better.

Name (required)

Email (required)

Website

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Feel free to leave a comment

top