You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Darren James <jd...@netflix.com> on 2008/01/25 00:04:04 UTC

tag writers resources?

Hi All,

Does anyone (know of/have any) resources that cover writing a tag?
I want to write a struts2 body tag that sets some applications specific
state before it emits it's body, and clears the state once the body has
been emitted.  Any tag-writers guides out there???


thanks in advance,

- darren.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag writers resources?

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Darren James wrote:
> Hi Joachim,
>
> This gives me something to start with.   Time to dig into the struts2
> source code to help me grok the sample you've provided.
>
> thanks,
>
>
That example creates a  Struts2 component that can be used as a JSP 
tag.  Also see usages of the org.apache.struts2.views.TagLibrary 
interface for how to make the model available to Velocity and Freemarker 
pages as well.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag writers resources?

Posted by Joachim Ansorg <ja...@ksi.gr>.
Hi Darren,
the example that I posted is just for JSP. But if you look at the 
struts2 examples it shouldn't be that hard to make a tag usable in 
freemarker or velocity as well.
As you can see the information available on writing native Struts2 tags 
is quite hard to find.
The most important reason for me writing a S2 tag has been the 
evaluation of the ognl expressions. But as Dale wrote the TagUtils class 
can probably be used for that in the standard JSP tags.

Joachim
>
> This gives me something to start with.   Time to dig into the struts2
> source code to help me grok the sample you've provided.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag writers resources?

Posted by Darren James <jd...@netflix.com>.
Hi Joachim,

This gives me something to start with.   Time to dig into the struts2
source code to help me grok the sample you've provided.

thanks,

- Darren.

Joachim Ansorg wrote:
>
> Hi Darren,
> writing a simple Struts2 tag is not that difficult. But getting 
> started is.
> The documentation of the tag api is not very good, imho. The best start
> is in my opinion to get the Struts2 sources and read the source code of
> the existing struts2 tags.
>
> I'm posting a simple example with this mail so it's in the archives for
> anybody. A tag writers guide would be a very useful resource for S2 users.
>
> For a S2 body you basically need:
>
> -A JSP tag implementation. e.g.:
>
> public class NameValueTag extends AbstractUITag{
>     private String editMode;
>
>     public Component getBean(ValueStack valueStack, HttpServletRequest
> httpServletRequest, HttpServletResponse httpServletResponse) {
>         return new NameValue(valueStack, httpServletRequest,
> httpServletResponse);
>     }
>
>     protected void populateParams() {
>         super.populateParams();
>         ((NameValue) component).setEditMode(editMode);
>     }
>
>     public String isEditMode() {
>         return editMode;
>     }
>
>     public void setEditMode(String editMode) {
>         this.editMode = editMode;
>     }
> }
>
> -A Struts2 component which is used by Struts. The JSP tags sets the
> attributed on the component. The annotations are later used for
> automatic .tld generation. I'm using Maven for that.
>
> @StrutsTag(name = "nameValue", description = "Inserts a formatted
> name-value html code construct.", tldTagClass =
> "gr.ksi.tags.ksiplugin.views.jsp.NameValueTag")
> public class NameValue extends ClosingUIBean {
>     private String editMode;
>
>     public NameValue(ValueStack valueStack, HttpServletRequest
> httpServletRequest, HttpServletResponse httpServletResponse) {
>         super(valueStack, httpServletRequest, httpServletResponse);
>     }
>
>     public String getDefaultOpenTemplate() {
>         return "ksinamevalue";
>     }
>
>     protected String getDefaultTemplate() {
>         return "ksinamevalue-end";
>     }
>
>     @StrutsTagAttribute(description = "Set to true to display edit boxes
> instead of text labels.", required = false, type = "Boolean")
>     public void setEditMode(String editMode) {
>         this.editMode = editMode;
>     }
>
>     public String isEditMode() {
>         return editMode;
>     }
>
>     protected void evaluateExtraParams() {
>         if (editMode != null) {
>             addParameter("editMode", findValue(editMode, Boolean.class));
>         }
>     }
> }
>
> And you need Freemarker templates which render the html. Have a look at
> the getDefaultTemplate() and getDefaultOpenTemplate() methods above.
> These point to the corresponding freemarker templates.
> The open template in this example is:
>
> [#ftl]
> [#if parameters.editMode?exists && parameters.editMode == true]
> <div class="name_value_edit">
>     <span class="name">
>         [#if parameters.label?exists] [@s.property
> value="parameters.label" /] [/#if]
>     </span>
>     <span class="value">
> [#else]
> <div class="name_value">
>     <span class="name">
>         [#if parameters.label?exists] [@s.property
> value="parameters.label" /] [/#if]
>     </span>
>     <span class="value">
> [/#if]
>
> The closing template is just:
> [#ftl]
> </ul>
>
> The rendered body is put in between.
>
> These examples are a full implementation of a S2 tag.
> If you want to place custom actions before and after the body rendering
> I think overriding ClosingUIBean.start and ClosingUIBean.end (don't
> remember the right name atm).
>
> Hope that helps,
> Joachim
> > Hi All,
> >
> > Does anyone (know of/have any) resources that cover writing a tag?
> > I want to write a struts2 body tag that sets some applications specific
> > state before it emits it's body, and clears the state once the body has
> > been emitted.  Any tag-writers guides out there???
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [struts] tag writers resources?

Posted by Joachim Ansorg <ja...@ksi.gr>.
Dale,
this is useful information for me. Thanks for pointing this out.
I'll make use of this class, I think.

Joachim
> P.P.S:  If your question is really "how do i evaluate tag arguments 
> that are really ognl expressions, then TagUtils.getStack() is probably 
> what you're looking for:
> http://struts.apache.org/2.x/struts2-core/apidocs/org/apache/struts2/views/jsp/TagUtils.html 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [struts] tag writers resources?

Posted by Dale Newfield <Da...@Newfield.org>.
Darren James wrote:
> What I was more interested in is some guidelines similar to what
> Joachim posted in writing a "struts2" tag.  And by that I mean one of
> those generic, technology independent beasts that can be used in
> Freemarker and Velocity as well as JSP.

I don't know how one writes freemarker tags or velocity tags.  I haven't 
studied Joachim's example in detail, but it didn't appear to be 
answering your question.  It appeared to me that it was an example of 
"how to write a tag that is implemented by freemarker" rather than "how 
to write a tag that is callable from freemarker".

If you're going to have a theme system, and want to be able to swap out 
templates at will, this is a reasonable solution.  If you just want a 
standard tag, I see no reason to call on a templating system inside the tag.

You should also be aware that the simplest way to implement a 
templatable tag (using jsp) is using a .tag file and a tagdir.  No java 
code required.

-Dale

P.S.:  Potentially useful pages if you need to call this tag from 
freemarker or velocity:

http://freemarker.org/docs/dgui_misc_userdefdir.html
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#velocimacros

P.P.S:  If your question is really "how do i evaluate tag arguments that 
are really ognl expressions, then TagUtils.getStack() is probably what 
you're looking for:
http://struts.apache.org/2.x/struts2-core/apidocs/org/apache/struts2/views/jsp/TagUtils.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [struts] tag writers resources?

Posted by Darren James <jd...@netflix.com>.
Hi Dale,

Yes, i'm familiar with writing JSP tags, although it's been quite
some time since i've done so.  It took me about 1/2 an hour to
dust off the cobwebs and write a JSP tag extending BodyTagSupport
that did what I wanted it to do.

What I was more interested in is some guidelines similar to what Joachim
posted in writing a "struts2" tag.  And by that I mean one of those generic,
technology independent beasts that can be used in Freemarker and Velocity
as well as JSP.

thanks,

- Darren.

Dale Newfield wrote:
>
> Joachim Ansorg wrote:
> > writing a simple Struts2 tag is not that difficult.
>
> A Tag is simply a java interface:
> http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/tagext/Tag.html
>
> The life cycle of a tag is discussed in that javadoc.
>
> There are a number of classes that make implementing your own pretty
> straight forward.  You'll probably be interested in "BodyTagSupport".
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [struts] tag writers resources?

Posted by Dale Newfield <Da...@Newfield.org>.
Joachim Ansorg wrote:
> writing a simple Struts2 tag is not that difficult.

A Tag is simply a java interface:
http://java.sun.com/javaee/5/docs/api/javax/servlet/jsp/tagext/Tag.html

The life cycle of a tag is discussed in that javadoc.

There are a number of classes that make implementing your own pretty 
straight forward.  You'll probably be interested in "BodyTagSupport".

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag writers resources?

Posted by Joachim Ansorg <ja...@ksi.gr>.
Hi Darren,
writing a simple Struts2 tag is not that difficult. But getting started is.
The documentation of the tag api is not very good, imho. The best start 
is in my opinion to get the Struts2 sources and read the source code of 
the existing struts2 tags.

I'm posting a simple example with this mail so it's in the archives for 
anybody. A tag writers guide would be a very useful resource for S2 users.

For a S2 body you basically need:

-A JSP tag implementation. e.g.:

public class NameValueTag extends AbstractUITag{
    private String editMode;

    public Component getBean(ValueStack valueStack, HttpServletRequest 
httpServletRequest, HttpServletResponse httpServletResponse) {
        return new NameValue(valueStack, httpServletRequest, 
httpServletResponse);
    }

    protected void populateParams() {
        super.populateParams();
        ((NameValue) component).setEditMode(editMode);
    }

    public String isEditMode() {
        return editMode;
    }

    public void setEditMode(String editMode) {
        this.editMode = editMode;
    }
}

-A Struts2 component which is used by Struts. The JSP tags sets the 
attributed on the component. The annotations are later used for 
automatic .tld generation. I'm using Maven for that.

@StrutsTag(name = "nameValue", description = "Inserts a formatted 
name-value html code construct.", tldTagClass = 
"gr.ksi.tags.ksiplugin.views.jsp.NameValueTag")
public class NameValue extends ClosingUIBean {
    private String editMode;

    public NameValue(ValueStack valueStack, HttpServletRequest 
httpServletRequest, HttpServletResponse httpServletResponse) {
        super(valueStack, httpServletRequest, httpServletResponse);
    }

    public String getDefaultOpenTemplate() {
        return "ksinamevalue";
    }

    protected String getDefaultTemplate() {
        return "ksinamevalue-end";
    }

    @StrutsTagAttribute(description = "Set to true to display edit boxes 
instead of text labels.", required = false, type = "Boolean")
    public void setEditMode(String editMode) {
        this.editMode = editMode;
    }

    public String isEditMode() {
        return editMode;
    }

    protected void evaluateExtraParams() {
        if (editMode != null) {
            addParameter("editMode", findValue(editMode, Boolean.class));
        }
    }
}

And you need Freemarker templates which render the html. Have a look at 
the getDefaultTemplate() and getDefaultOpenTemplate() methods above. 
These point to the corresponding freemarker templates.
The open template in this example is:

[#ftl]
[#if parameters.editMode?exists && parameters.editMode == true]
<div class="name_value_edit">
    <span class="name">
        [#if parameters.label?exists] [@s.property 
value="parameters.label" /] [/#if]
    </span>
    <span class="value">
[#else]
<div class="name_value">
    <span class="name">
        [#if parameters.label?exists] [@s.property 
value="parameters.label" /] [/#if]
    </span>
    <span class="value">
[/#if]

The closing template is just:
[#ftl]
</ul>

The rendered body is put in between.

These examples are a full implementation of a S2 tag.
If you want to place custom actions before and after the body rendering 
I think overriding ClosingUIBean.start and ClosingUIBean.end (don't 
remember the right name atm).

Hope that helps,
Joachim
> Hi All,
>
> Does anyone (know of/have any) resources that cover writing a tag?
> I want to write a struts2 body tag that sets some applications specific
> state before it emits it's body, and clears the state once the body has
> been emitted.  Any tag-writers guides out there???

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: tag writers resources?

Posted by Dave Newton <ne...@yahoo.com>.
Without knowing exactly what you mean it's hard to know where to point you.

Have you looked at existing tag sources? Have you looked at any of the normal
JSP custom tag documentation and tutorials in the wild?

d.

--- Darren James <jd...@netflix.com> wrote:

> Hi All,
> 
> Does anyone (know of/have any) resources that cover writing a tag?
> I want to write a struts2 body tag that sets some applications specific
> state before it emits it's body, and clears the state once the body has
> been emitted.  Any tag-writers guides out there???
> 
> 
> thanks in advance,
> 
> - darren.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org