• 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

Taglib that translates into a series of other taglib calls

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone know how to accomplish the following with a JSP taglib?

I'd like to selectively show one of two struts tags depending on whether an OGNL expression evaluates to true. I don't need to do any evaluation myself, merely convert my tag into a more complicated series of them. i.e.

<my:textorlabel param1="foo" param2="bar" enabled="baz" />

needs to get converted to something like:

<s:if test="baz">
<s:textfield param1="foo" param2="bar" />
</s:if>
<s:else>
<s:label param1="foo" param2="bar" />
</s:else>

Any simple way of doing this with my own custom textorlabel taglib?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, write it as a JSP 2.0+ tag file.
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Sure, write it as a JSP 2.0+ tag file.


Could you elaborate further? I've written previous tags by creating a class extending TagSupport and putting logic into doStartTag(), but this has been for a simple tag, nothing like the above.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP-based custom tags, not Java-based.
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:JSP-based custom tags, not Java-based.


Ok, I've made a textorlabel.tag with this content:


Attempt to call it in one of my JSP's by:


gives this error:

com.ibm.ws.jsp.translator.JspTranslationException: JSPG0228E: Exception caught while translating /WEB-INF/jsp/newbusiness/productOptions.jsp: error in statically included file
/WEB-INF/tags/textorlabel.tag(8,1) --> JSPG0124E: Custom tag attribute test cannot be runtime expression. value: "[${enabled}]"

Any idea how to reference a parameter I pass in? Tag file tutorials seem to suggest using ${} is the way.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But S2 documentation seems to suggest that we disallow JSP EL due to the security risk. So use OGNL all the way; there's a FAQ entry on this I think.
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:But S2 documentation seems to suggest that we disallow JSP EL due to the security risk. So use OGNL all the way; there's a FAQ entry on this I think.


Right, I can get at the parameters made when calling my tag by using #attr.enabled, #attr.name etc etc.

However, as these are OGNL expressions, I need a way to evaluate them. e.g.

<s:if test="#attr.enabled"> needs to be something like <s:if test="%{#attr.enabled}"> as #attr.enabled itself contains an OGNL expression like "object.enabled" that should be resolved against the value stack. But, surprise surprise, this doesn't work. Nesting %{'s doesn't cause the inner result to get evaluated as if it were an OGNL expression.

So, where's the OGNL eval function?
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Xolani Nkosi wrote:

David Newton wrote:But S2 documentation seems to suggest that we disallow JSP EL due to the security risk. So use OGNL all the way; there's a FAQ entry on this I think.


Right, I can get at the parameters made when calling my tag by using #attr.enabled, #attr.name etc etc.

However, as these are OGNL expressions, I need a way to evaluate them. e.g.

<s:if test="#attr.enabled"> needs to be something like <s:if test="%{#attr.enabled}"> as #attr.enabled itself contains an OGNL expression like "object.enabled" that should be resolved against the value stack. But, surprise surprise, this doesn't work. Nesting %{'s doesn't cause the inner result to get evaluated as if it were an OGNL expression.

So, where's the OGNL eval function?



Right, this thread seems to detail my problem: http://old.nabble.com/Attribute-OGNL-evaluation-issue-on-struts-tag-in-tag-file-td21732140.html

Someone claims to have found the solution at http://forums.opensymphony.com/thread.jspa?messageID=6278131 but it seems the opensymphony forums aren't around any more. Anyone got a lead on where this solution now is?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Xolani Nkosi wrote:

Xolani Nkosi wrote:

David Newton wrote:But S2 documentation seems to suggest that we disallow JSP EL due to the security risk. So use OGNL all the way; there's a FAQ entry on this I think.


Right, I can get at the parameters made when calling my tag by using #attr.enabled, #attr.name etc etc.

However, as these are OGNL expressions, I need a way to evaluate them. e.g.

<s:if test="#attr.enabled"> needs to be something like <s:if test="%{#attr.enabled}"> as #attr.enabled itself contains an OGNL expression like "object.enabled" that should be resolved against the value stack. But, surprise surprise, this doesn't work. Nesting %{'s doesn't cause the inner result to get evaluated as if it were an OGNL expression.

So, where's the OGNL eval function?



Right, this thread seems to detail my problem: http://old.nabble.com/Attribute-OGNL-evaluation-issue-on-struts-tag-in-tag-file-td21732140.html

Someone claims to have found the solution at http://forums.opensymphony.com/thread.jspa?messageID=6278131 but it seems the opensymphony forums aren't around any more. Anyone got a lead on where this solution now is?



Hey, I am having a similar issue (posted in http://old.nabble.com/How-can-one-use-OGNL-on-custom-.tag-files--td27880257.html ). Did you manage to find a solution to your problem?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't double-evaluated OGNL (well, you can, by using OGNL's static eval method, but I seriously don't think it's the best solution).
 
Miguel Almeida
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:You can't double-evaluated OGNL (well, you can, by using OGNL's static eval method, but I seriously don't think it's the best solution).



David, do you have any suggestion regarding the use of a custom .tag in my case?
The idea is to use:


which points to a (currently not working due to the above reason) .tag file:




By the way, congratulations on your "Apache Struts 2" Packt book. It's a great S2 reference and I've recommended it to everyone starting to learn Struts!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly, I'd use a JavaScript calendar widget, and use Apache Commons date utility to try a collection of date formats if JavaScript is disabled.

Thanks for the kind words :)
 
Xolani Nkosi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miguel Almeida wrote:Hey, I am having a similar issue (posted in http://old.nabble.com/How-can-one-use-OGNL-on-custom-.tag-files--td27880257.html ). Did you manage to find a solution to your problem?



Yes, I came up with one myself. It makes the tag a bit more messy, but works by evaluating the attributes containing OGNL, and pushing the result back onto the stack with the suffix OGNL, which can then be used in further tags. It'll only evaluate things that start %{ and end }

textorlabel.tag
 
Miguel Almeida
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Honestly, I'd use a JavaScript calendar widget, and use Apache Commons date utility to try a collection of date formats if JavaScript is disabled.

Thanks for the kind words :)



Hey David,

I am already using a javascript calendar widget (jquery's datepicker), but I am stuck with the same problem: I could

<s:textfield id="%{#attr.dateField}" name="%{#attr.dateField}.date" cssClass="calendar %{#attr.dateField}.format" />



So I could set the format based on the css class, but I need the actual evaluation of "x.format".

Eg:
if dateFied=death.date, I'd need the value of death.date.format, ie, something like "MM-yyyy", and not the string "death.date.format".

I've been going in circles for a while with this, but I'm finding it very hard to do something so trivial like this.

The following code works on the JSP. But I can't encapsulate it in a .tag file, which is cumbersome when you re-use it a lot and notice all that is changing is the "death.deathDate" (date object which will hold the .date and .dateFormat) and the label...

reply
    Bookmark Topic Watch Topic
  • New Topic