Ben wrote:

Hi

Anyone knows any example on how to use ImageButtonBean in
LookupDispatchAction? Or can someone give me an example
implementation? Thanks.

Regards,
Ben

Hi, Ben,


I have in the past relied upon readers to do their own thinking on this sort of thing. Let me walk you through it, however, if you like.




Let me explain in more detail, Ben, what is wrong with mixing these systems, and what is wrong with them individually. Let's see, first, how they each work. *_/The key to all these devices is that they have to have a means of getting the name in a request name/value pair in order to get the value. /_* This is the reverse of what we normally do in web work. So, these devices may seem strange at first, but they are rather simple.



1.  DispatchAction



DispatchAction uses a URL to pass the following name/value pair, as an example: submitSubscription?method=update. Now, we have to have away to know that the name in the name/value pair method=update is, so we use our action mapping to make the value of the attribute called "parameter" be this name, so that in your action mapping parameter='method'. So, we get the value of the action mapping and use that to get the value "update" from the request parameters, which we then use in reflection to call the method we want. Simple!



2.  LookupDispatchAction



This is more complicated. Not much though. This really should be called "ReverseLookupDispatchAction" to identify what it does. LookupDispatch uses the fact that the following code in a file called test.jsp:



<html>
<body>
<form method='get' action='test2.jsp'>
   <input type='submit' name='method' value='Delete It'>
</form>
</body>
</html>



will render the following URL:http://www.whatever.com/test.jsp?method=Delete+It. So, LookupDispatchAction uses the following code:



<html:html>
<form method='get' action='test2.jsp'>
<html:submit property='method'><html:message key='button.delete'></html:submit>
</form>
</html:html>




where button.delete renders "Delete It" as the value, yielding the same URL, i.e. http://www.whatever.com/test.jsp?method=Delete+It. The difference is that "Delete It" here comes from application resources. Believe it or not, LookupDispatchAction createds a reverse map from the users locale and the relevant action resources property file so that "Delete It" is not the value but is the key and "button.delete" is not the key but the value. Then, you will have coded another map into your LookupDispatchAction which has "button.delete" as the key and "delete" as the value. This is the rather circuitous process by which the command, method, etc. to call by reflection is obtained by LookupDispatchAction.



3.  ImageButtonBean



ImageButtonBean is primarily associated with images. This device utilizes the fact that <input type='image' name='button.create' src='create.gif'> will send the following URL with a "get" form:http://www.whatever.com/test.jsp?button.create.x=30&button.create.y=8. We can use reflection and the Struts framework to use this URL to call [YourActionForm].getButton().getCreate().getX(). If getCreate() returns a button CreateImageButtonBean it will show that getX() return a non-null and, so, that the method "create" was wanted by the user. There has to be, of course, corresponding setter actions for the beans. Thus, reflection is used to mine or to obtain the value "create" from the name "button.create" in the request object.



These are very heavy handed means. Note that you have to create a button per method with the ImageButtonBean. Also, notice that you don't need ImageButtonBean if you use LookupDispatchMethod. You could use ImageButtonBean in conjunction with DispatchAction, but that is not adviable either.



With all this in mind, I highly recommend that you pursue one of the five approaches in my www.michaelmcgrady.com coding suggestions to this problem. There really is a simpler way. I recommend the DispatchUtil approach, which combines looking up the name with an ".x" suffix and then employing reflection on an Action class. However, if you want something decoupled from Struts, then the ButtonTagUtil would be best. The other solutions, except my version of DispatchAction (once called "SimpleDispatchAction"), are really just examples of how you could better do what, e.g. ImageButtonBean does and avoid heavy object creation problems.



I hope this helps.



Michael McGrady

Reply via email to