View Full Version : Parametrized Controller?
TSH
Aug 13th, 2007, 09:46 AM
Hello,
I have defined a generic controller:
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="index.html">indexController</prop>
<prop key="dvds/**/*.html">itemController</prop>
<prop key="books/**/*.html">itemController</prop>
</props>
</property>
</bean>
My itemController connects to a database and gets all dvds or books. But how do I tell the controller which type of item to look for?
Is it possible to pass a parameter "dvd" to the controller? Of course I can parse the request url, but thats kind of awkward I think.
dr_pompeii
Aug 13th, 2007, 09:58 AM
My itemController connects to a database and gets all dvds or books.
i hope that you controller call some service/BO and this a DAO and returns the list
that a controller call directly a db is a bad practice
But how do I tell the controller which type of item to look for?
is not an option already selected in your jsp form before to does the submit?
Is it possible to pass a parameter "dvd" to the controller?
String idBook = (Strring) request.getParameter("idBook"); ???
but thats kind of awkward I think.
why did you say that?
regards
TSH
Aug 13th, 2007, 10:09 AM
Thank you for your quick reply. My controller doesn't call the db directly.
is not an option already selected in your jsp form before to does the submit?
There is no "submit". I want it to work even when the user types www.xyz.com/dvds/abc.html into the browser field.
Spring and its dependency injection is so elegant that I hope(d) parsing an url will not be neccessary. I thought there would be a possible xml-solution inside the mapping.
JEisen
Aug 13th, 2007, 12:06 PM
You could also change the beans to itemControllerBooks and itemControllerDvds, then define them to use the same class but pass a different property. I would pass either a string that you'd switch on or a service object for maximum OOP.
<bean id="itemControllerBooks" class="com.myproject.controllers.ItemController">
<property name="itemFetcher">
<ref local="bookFetcher"/>
</property>
</bean>
<bean id="itemControllerDvds" class="com.myproject.controllers.ItemController">
<property name="itemFetcher">
<ref local="dvdFetcher"/>
</property>
</bean>
...
[in ItemController]:
List results = itemFetcher.fetchItems();
So here you're injecting the dependency and the type of item to retrieve without having to write more than one ItemController. Then again, you do need a different ItemFetcher for each item. Otherwise, you can put the logic in your DAO/Service class and do it with a String:
<bean id="itemControllerBooks" class="com.myproject.controllers.ItemController">
<property name="item" value="books"/>
</bean>
<bean id="itemControllerDvds" class="com.myproject.controllers.ItemController">
<property name="item" value="dvds"/>
</bean>
[in ItemController]:
List results = service.getItems(item);
JEisen
Aug 13th, 2007, 12:17 PM
TSH is quite right, parsing the URL is very awkward and shouldn't be in proper dependency-injected code (not that I haven't had to use it due to a limitation in Spring, but it should be avoided).
Let's say that, at some future point, I decided to sell downloadable movie content as well as DVDs (or even VHS or UMD). Well, with the current scheme, I still need to go to site.com/dvds/store/order.html. I'd like to change this to be a more clear URL: site.com/movies/store/order.html. With a properly designed Spring config, I could just change "dvds/**/*.html" to "movies/**/*.html", upload the XML, and I'm done. Everything auto-updates.
If I had written parsing code into the Java, though, I'd now have to go into the code, change it, recompile, test, change the XML, upload the whole thing and potentially recompile on the server. Editing only the XML introduces far less chance of something going wrong and makes the mapping easily fixable.
(Not that you should upload directly to production in any case. But you get the idea.)
Jörg Heinicke
Aug 13th, 2007, 10:19 PM
My itemController connects to a database and gets all dvds or books. But how do I tell the controller which type of item to look for?
I'd either go with JEisen's approach or consider using the MultiActionController (http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html#mvc-controller-multiaction). There you have a MethodNameResolver which can construct and encapsulate the method name from the Url. The MultiActionController works a bit different than the form or command controllers though and has the one or the other limitation (better said missing support, which you have to add yourself if needed, e.g. the usage of PropertyEditors).
Of course I can parse the request url, but thats kind of awkward I think.why did you say that?
Because it is ;)
Joerg
TSH
Aug 14th, 2007, 04:56 AM
Thank you all for your suggestions. I will try JEisens 2nd approach. That seems to be the one that matches my "simplicity first" philosophy best ;-)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.