aspose file tools*
The moose likes Struts and the fly likes Taglib that translates into a series of other taglib calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Taglib that translates into a series of other taglib calls" Watch "Taglib that translates into a series of other taglib calls" New topic
Author

Taglib that translates into a series of other taglib calls

Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sure, write it as a JSP 2.0+ tag file.
Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

JSP-based custom tags, not Java-based.
Xolani Nkosi
Ranch Hand

Joined: Apr 29, 2009
Posts: 32
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 29, 2009
Posts: 32
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

Joined: Apr 29, 2009
Posts: 32
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?
Miguel Almeida
Greenhorn

Joined: Mar 12, 2010
Posts: 9
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 12, 2010
Posts: 9
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 29, 2009
Posts: 32
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

Joined: Mar 12, 2010
Posts: 9
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...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Taglib that translates into a series of other taglib calls