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] [EMAIL PROTECTED] value="parameters.label" /] [/#if]
   </span>
   <span class="value">
[#else]
<div class="name_value">
   <span class="name">
[#if parameters.label?exists] [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to