• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass attribute to a custom tag only if it evaluates to a non null value

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to invoke a html tag and I want the attribute to be passed to the tag only if it evaluates to a non-null value in EL.

Currently when I write:
<html:select>
<jsp:attribute name="alt" > ${list1Alt} </jsp:attribute>
<jsp:attribute name="property"> ${list1Property} </jsp:attribute>
</html:select>

The generated HTML code contains:
<select name="name1" alt=""></select>

I do not want the alt="" to appear since value of ${list1Alt} was null. (Effectively I don't want the attribute to apply if its value evaluates to null)

Can somebody resolve this issue.

 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all the value you get from a EL would be empty if null.

Where are you trying to make a condition?

It wouldn't happen automatically. You have to use an if condition to make it like that either directly or in some custom tag.

Cheers.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

I am trying to write a tag file while contains two <html:select ...> tags.

To ensure that all attributes of <html:select ...> are available to the user invoking my tag I defined then as attributes to the tag file.
Now if I try to pass all the received attributes (which can be null, or empty due to EL) to <html:select ...> series of exceptions are thrown since all attribute setters of <html:select tag> are invoked.

If i try to do


obviously <jsp:attribute ...>will apply to <c:if ...>.

I am looking for a way to set attributes to the tag only if they are not null or empty.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you send the code of your tag file?
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

Here is a snip of the tag file...



Using scriptlet-expression instead of EL serves better, as unlike EL scriplet passes null values for null attributes. But setter of the attribute is still invoked. Passing null to name attribute of html:select causes exception...

One way I can think of is extend SelectTag and intercept the setters... but is there a better way?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Gujjar:
Using scriptlet-expression instead of EL serves better, as unlike EL scriplet passes null values for null attributes.



Why you want null? why not empty string?
Moreover, I am unable to find where are you checking for emptiness or null?
Where is the problem in using original tags? I mean, is there any other reason to do that or just that error is the sole reason?
[ May 07, 2007: Message edited by: Adeel Ansari ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

<html:select>

<c:if test=${!empty list1Alt}>
<jsp:attribute name="alt" > ${list1Alt} </jsp:attribute>
</c:if>

<c:if test=${!empty list1Property}>
<jsp:attribute name="property"> ${list1Property} </jsp:attribute>
</c:if>

</html:select>
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

To clarify on your questions

Why you want null? why not empty string?
Moreover, I am unable to find where are you checking for emptiness or null?



The big picture is, I am authoring a web component composed of a tag file.
The tag file is expected to contain two list boxes (made up of html:select for struts compatibility). Further it is expected to contain slurry of buttons to move stuff from one list to another.

Now to answer your question, I merely want to pass on the attributes passed to my tag file to html:select. I neither want to pass null or blank string to html:select. I don�t want to pass an attribute to html:select tag if no equivalent attribute was passed to my tag file.
Currently I am passing null by usage of scriptlet as it is less catastrophic than passing blank strings to struts html:select tag. Struts expects few attributes to be mutually exclusive, passing blanks by usage of EL sets all attributes causing exception. Further passing blank or null to certain attributes like name of html:select poses other disasters.

Adding the if condition to selectively pass attributes to html:select is exactly my concern.



Hi Hari,


The below code as per your suggestion


causes attribute alt to be applied to c:if instead of html:select.
 
Hari Maha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one works, check if its useful

 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hari Maha:



Exactly.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari,Adeel,

The code

definately works.
But I need to be create select box compatible with struts select tag, since user of my component uses struts and wants to assign it a form bean property, further he wants all the benefits provided by struts.

If I try to do

it obviously results in exception.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you would get an exception? Since you are not getting null, neither empty string.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because by JSP rules its not legal to nest a tag in tag this way

 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Gujjar:
Because by JSP rules its not legal to nest a tag in tag this way



Oops!! I missed it. Correct it like below.

 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But even this wouldn't solve your issue.

Why don't you create a variable and set it after checking each and every attribute then place that variable into the original tag.

Is it making sound to you?
[ May 08, 2007: Message edited by: Adeel Ansari ]
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

The suggested code results in below HTML output (using Tomcat)



Even if it would have worked, it would set the alt attribute to blank string.

My issue is: How to set attributes to a custom tag based on some condition.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very simply you cannot use a custom tag as the attribute of another custom tag. if you are using a JSP 2.0 container you should be able to just use an EL expression with the ternary operator to make a simple test. Since using EL with Struts tags is not a general JSP issue, I've moved this topic to the Struts forum.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Gujjar:
My issue is: [b]How to set attributes to a custom tag based on some condition.



Read my last post. Thanks.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikas,

What I'd like you to do is forget all this rhetoric about JSTL and just tell us in as simple as possible terms what you're trying to accomplish. What do you want the page to look like if the value is not null? What do you want it to look like if the value is null?

I've read the previous threads, but it gets tangled up in what can and can't be done, and I don't have a clear picture of what you're trying to accomplish.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Here is my issue is concise terms.
I have a tag file which has number of elements including two <html:select ..> tags.

I have declared all attributes which can be passed to these <html:select ...> tags as attributes to the tag file as below:



Most of these attributes on html:select are optional hence I have marked them as required="false" too at my end.

Now, I want to pass all the corresponding attributes passed to my tag file to <html:select ...>. How do I achieve this?

Note that if an attribute list1Alt is not passed to the tag file
${list1Alt} evaluates to blank
<%=list1Alt%> evaluates to null
[ May 11, 2007: Message edited by: Vikas Gujjar ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adeel Ansari:

Why don't you create a variable and set it after checking each and every attribute then place that variable into the original tag.

Is it making sense to you?

 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Gujjar:

Now, I want to pass all the corresponding attributes passed to my tag file to <html:struts ...>. How do I achieve this?


You don't. It is not possible to have one custom tag render another custom tag and in turn have that tag render HTML. If you write a custom tag, it must render plain old HTML.

What I'd suggest is that you have your custom tag handler extend org.apache.struts.taglib.html.SelectTag. That way you don't have to duplicate what Struts has already done, and you can just add whatever additional functionality you need.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

Why don't you create a variable and set it after checking each and every attribute then place that variable into the original tag.

Is it making sense to you?



Can you help me with a code example? I can imagine to do what you mentioned with a html <SELECT .../> but I am not able to figure out any way to do that for <html:select .../>

Hi Merrill

What I'd suggest is that you have your custom tag handler extend org.apache.struts.taglib.html.SelectTag.



This has been the best idea to fix this so far.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Merrill Higginson:
You don't. It is not possible to have one custom tag render another custom tag and in turn have that tag render HTML. If you write a custom tag, it must render plain old HTML.



We are doing it using spring tags and some of our own tags. Below are the examples.



 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel/Merrill,

You don't. It is not possible to have one custom tag render another custom tag and in turn have that tag render HTML. If you write a custom tag, it must render plain old HTML.



I agree we can render custom tag from custom tag by using a tag file. I have been doing that all along so far, the question is how can we pass attributes selectively to a custom tag.

Extending the SelectTag and calling the extended selectTag from the tag file does solve my problem in a slightly cumbersome way.

I hope someday the JSP spec is extend to do



This can serve as an invaluable help in variety of circumstances.

Thanks
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Gujjar:
I hope someday the JSP spec is extend to do


This can serve as an invaluable help in variety of circumstances.



We can do this with the current thing, I believe.
BTW, It will always give you true.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

We can do this with the current thing, I believe.



Can you pass me a code example as how to do it?

Thanks
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys are right. My assumption that one custom tag can't render another was based on experience working with Java tag handlers, not with tag files. That's what I like about being plugged in to the developer community through forums like this: It helps clear out of my brain cache the repository of outmoded assumptions that may no longer be true.

Good luck with your project, Vikas.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Try it out.
 
Vikas Gujjar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

I cannot use plain HTML tags as below



I need to pass attributes to html:select

My question is how to pass attributes conditionally to custom tag.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you really not replace the tag yourself? Just curious.

Should I give you the exact code that allow you to copy from here and paste there?
[ May 17, 2007: Message edited by: Adeel Ansari ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I don't like to revive a dead thread, but I actually run into the same issue. I have an struts html:form here where some attributes are added to using jsp:attribute tags. The attribute itself is filled using tiles:getAsString and may be an empty string. But struts will only test against null in the FormTag class, so if the attribute is present, it will be used, even it is empty. See org.apache.struts.taglib.html.FormTag#renderFormStartElement() at Line 506 for the attribute method.

so how can I omit to set the jsp:attribute if I can't have an c:if around?

code example:


Cheers,
SF
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic