Announcement Announcement Module
No announcement yet.
Role based view Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

    ...creator role specific JSPs
    ...approver role specific JSPs
    ...authorizer role specific JSPs
    ...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" />

    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?


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


    • #3
      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
        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
          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
            For something like that you can probably use a HandlerInterceptor overriding the postHandle method and modifying the view name as necessary.


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