Feb 11th, 2010, 03:33 PM
Missing the connection between DispatcherPortlet and Controller
Spring Portlet MVC 2.5.6
I am missing the connection...if I am in class MyController extends AbstractController, how do I get at the DispatcherPortlet object? For example, if I wanted to call getUsernameForRequest() from the DispatcherPortlet object how would I do that? I understand DispatcherPortlet's job is to do as it's name implies, which in this case is to dispatch to MyController, I am just trying to understand. Thanks.
Feb 11th, 2010, 06:00 PM
AFAIK there is no link back to the DispatcherPortlet from a Controller, and there really shouldn't be either.
The method you have referred to is an example of something that might have been useful to have in PortletUtils, but it isn't. You could implement the exact same thing as a utility in a couple of lines of code if you need it - the main advantage of this method is the fallback option to use the portlet user attributes as another source of username if the request doesn't contain it (PortletRequest.getRemoteUser or PortletRequest.getUserPrincipal). Even if there is a link back to the DispatcherPortlet somewhere, you won't be able to access this method since it is protected.
Feb 12th, 2010, 08:54 AM
What I had in mind was to extend DispatcherPortlet. So I would, for example, have class MyPortlet extends DispatcherPortlet. MyPortlet class has some utility type methods in it. I have done the same in the past, prior to using Spring, when extending javax.portlet.GenericPortlet. I could just do getSomeMethod() from any portlet, the method was always there without any effort. I was hoping to do the same for my Controllers.
I will have approx 7 distinct portlets each having approx 5 controllers. So, based on my limited understanding of Spring, I would have to either inject this MyPortlet class 35 times in the bean config files or programmatically request it 35 times in each controller, either of which is ugly as it's up to each developer to make sure they are doing so. Isn't there a better way to accomplish this?
Feb 12th, 2010, 02:20 PM
I think you would be better to provide your own base Controller implementation(s) (e.g. extend SimpleFormContoller, etc) rather than trying to do this in the DispatcherPortlet.
If you can use the new @Controller annotation configuration approach then you have greater flexibility since you don't have to extend the Spring base classes, so it is much easier to provide your own base class containing these sorts of utilities.
Another alternative is to use a static utility class.
Tags for this Thread