• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7976
  • Last Modified:

Struts <html:link> and sending parameters

Hi,
  I've read a few answers to this question but I'm hoping there is a way to do it without scriplets. The simple reason being I'm not designing the pages and I'd like to keep it as clean as possible for the designer :)


Is there any way to simply specify a parameter? For example :


<html:link action="ShowStuff">
<parameter name="url"  value="www.test.com"/>
Here is the link text
</html:link>


I know this won't work, it's just an example of what I'm hoping exists :)



Any takers?
0
petepalmer
Asked:
petepalmer
  • 6
  • 5
1 Solution
 
boonlengCommented:
You can use JSTL if dont want to use scriptlet.
Example:

<%@ taglib uri="/tags/struts-html"  prefix="html" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<jsp:useBean id="map" class="java.util.HashMap" scope="page">
    <c:set target="${map}" property="url1" value="www.site1.com"/>
    <c:set target="${map}" property="url2" value="www.site2.com"/>
</jsp:useBean>

<html:link action="ShowStuff" name="map">Here is the link text</html:link>
0
 
petepalmerAuthor Commented:
So there's no way of doing it natively with struts?

The other problem is that each link would have a different parameter (i.e I would be iterating through a list of links ) so I imagine using a HashMap would be a bit of a pain....

wonder why they didn't think of this in their tag library? :)
0
 
boonlengCommented:
If you have have only one parameter in the url, then you dont need to use HashMap. You can loop it directly.
Example:

<logic:iterate id="value" name="arraylist">
    <html:link action="ShowStuff" paramId="url" paramName="value">Here is the link text</html:link>
</logic:iterate>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petepalmerAuthor Commented:
Very close - but the problem is that the value of paramName needs to be the same as the contents between the link tags i.e the same url  :)

0
 
boonlengCommented:
Use <bean:write> to output the value:

<logic:iterate id="value" name="arraylist">
    <html:link action="ShowStuff" paramId="url" paramName="value"><bean:write name="value"/></html:link>
</logic:iterate>
0
 
petepalmerAuthor Commented:
perfect example - nice one :)
0
 
petepalmerAuthor Commented:
Slight problem :)

It's nearly working perfectly - but I need "url"  to be the contents of the "url" property on the "feed" bean... any suggestions?
0
 
boonlengCommented:
You can specify the property name on the bean to the atttibute 'property'. If not specified, it will use the attribute 'name' as the value (e.g. previous example).

<logic:iterate id="feed" name="listOfBeans">
    <html:link action="ShowStuff" paramId="url" paramName="feed" paramProperty="url">
    <bean:write name="feedbean" property="url"/>
    </html:link>
</logic:iterate>
0
 
petepalmerAuthor Commented:
Thanks again - that is working beautifully now :)
0
 
petepalmerAuthor Commented:
Is it possible to use the <html:link> tag to link to a dynamic URL? i.e the url would be a property of a bean but instead of transfering control to a fixed location, I'd want each one to be a different url.

Maybe I should open a new question for that :)
0
 
boonlengCommented:
The <html:link> tag allows to link to dynamic url (see http://struts.apache.org/userGuide/struts-html.html#link), but if the url is from a bean, then will require to write scriptlet. You can use the following example if you prefer no scriptlet.

<logic:iterate id="feed" name="listOfBeans">
   <a href="<bean:write name="feedbean" property="url"/>"><bean:write name="feedbean" property="url"/></a>
</logic:iterate>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now