Results 1 to 7 of 7

Thread: Role based view

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Default Role based view

    Hello folks,

    I have a legacy web app that I am trying to upgrade to Spring MVC. The last hurdle I have to jump is getting the correct JSP page to load based on the user's role. The JSP structure is as such:

    /jsp/creator
    ...creator role specific JSPs
    /jsp/approver
    ...approver role specific JSPs
    /jsp/authorizer
    ...authorizer role specific JSPs
    /jsp/common
    ...JSPs common to all roles

    Based on the logged in user's role, I need to check in the appropriate role-based directory for the page. If it exists, I should render the role-specific page, if it doesn't exist I should render the page in the /jsp/common directory.

    Here's My app-servlet.xml file:

    <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />
    <context:annotation-config />
    <context:component-scan base-package="controller" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter"/>
    <bean id="jstlViewResolver"
    class="org.springframework.web.servlet.view.UrlBas edViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew" />
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>

    My guess is that I have to overload a method in either the UrlBasedViewResolver or JstlView, but I am not sure where to look. Can anyone point me in the right direction?

    Thanks,
    Matt

  2. #2
    Join Date
    Oct 2008
    Location
    Delhi, India
    Posts
    163

    Default

    Why don't you just return the appropriate view from your controller based on the user's role?

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Default

    Wouldn't I have to apply some sort of logic to every one of my controller methods to determine the user's role, then choose which is the appropriate view?

    If I can implement a custom View class that can figure that part out for me in one spot, I'd rather go that route.

  4. #4
    Join Date
    Oct 2008
    Location
    Delhi, India
    Posts
    163

    Default

    I was under the impression only one of your controller was going to return such a view. I'm not sure how you'd go ahead and implement it all in one view because the views will vary per controller anyway. For eg, Controller1 may return view1 for role1 and view2 for role2 while controller2 will return view3 for role1 and view4 for role2. Wouldn't you have to hardcode the logic per controller anyway?

  5. #5
    Join Date
    Sep 2008
    Posts
    4

    Default

    Oh sorry if I didn't explain that adequately.

    Every single action needs this same consideration because based on the role, they will see the page with slightly differently content.

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    For something like that you can probably use a HandlerInterceptor overriding the postHandle method and modifying the view name as necessary.

  7. #7
    Join Date
    Sep 2008
    Posts
    4

    Default

    OH! Great idea, that was simple to implement with a HandlerInterceptor....thanks Rossen!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •