-
Feb 16th, 2009, 03:46 PM
#1
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
-
Feb 16th, 2009, 11:20 PM
#2
Why don't you just return the appropriate view from your controller based on the user's role?
-
Feb 17th, 2009, 12:19 PM
#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.
-
Feb 17th, 2009, 11:07 PM
#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?
-
Feb 18th, 2009, 09:39 AM
#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.
-
Feb 18th, 2009, 05:29 PM
#6
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
#7
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
-
Forum Rules