Feb 16th, 2009, 03:46 PM
Role based view
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:component-scan base-package="controller" />
<bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter"/>
<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?
Feb 16th, 2009, 11:20 PM
Why don't you just return the appropriate view from your controller based on the user's role?
Feb 17th, 2009, 12:19 PM
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.
Feb 17th, 2009, 11:07 PM
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?
Feb 18th, 2009, 09:39 AM
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.
Feb 18th, 2009, 05:29 PM
For something like that you can probably use a HandlerInterceptor overriding the postHandle method and modifying the view name as necessary.
Feb 19th, 2009, 10:00 AM
OH! Great idea, that was simple to implement with a HandlerInterceptor....thanks Rossen!