Return-Path: Delivered-To: apmail-struts-user-archive@www.apache.org Received: (qmail 53392 invoked from network); 28 Jan 2008 19:26:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Jan 2008 19:26:45 -0000 Received: (qmail 13045 invoked by uid 500); 28 Jan 2008 19:26:26 -0000 Delivered-To: apmail-struts-user-archive@struts.apache.org Received: (qmail 13015 invoked by uid 500); 28 Jan 2008 19:26:26 -0000 Mailing-List: contact user-help@struts.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Struts Users Mailing List" Reply-To: "Struts Users Mailing List" Delivered-To: mailing list user@struts.apache.org Received: (qmail 13004 invoked by uid 99); 28 Jan 2008 19:26:26 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jan 2008 11:26:26 -0800 X-ASF-Spam-Status: No, hits=-1.0 required=10.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of jdjames@netflix.com designates 208.75.77.145 as permitted sender) Received: from [208.75.77.145] (HELO mx2.netflix.com) (208.75.77.145) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jan 2008 19:26:13 +0000 Received: from message.netflix.com (exchangeav [10.64.32.68]) by mx2.netflix.com (8.12.11.20060308/8.12.11) with ESMTP id m0SJUnCg031022 for ; Mon, 28 Jan 2008 11:30:58 -0800 Received: from Superfly.netflix.com ([10.64.32.70]) by message.netflix.com with Microsoft SMTPSVC(6.0.3790.1830); Mon, 28 Jan 2008 11:25:56 -0800 Received: from lgmac-djames.netflix.com ([10.2.229.117]) by Superfly.netflix.com with Microsoft SMTPSVC(6.0.3790.1830); Mon, 28 Jan 2008 11:25:56 -0800 Message-ID: <479E2D26.1090104@netflix.com> Date: Mon, 28 Jan 2008 11:29:42 -0800 From: Darren James User-Agent: Thunderbird 2.0.0.9 (Macintosh/20071031) MIME-Version: 1.0 To: Struts Users Mailing List Subject: Re: tag writers resources? References: <479DE6EF.5090107@ksi.gr> In-Reply-To: <479DE6EF.5090107@ksi.gr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 28 Jan 2008 19:25:56.0563 (UTC) FILETIME=[9AFA6230:01C861E3] X-Language-Identified: TRUE X-Virus-Checked: Checked by ClamAV on apache.org 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] >
> > [#if parameters.label?exists] [@s.property > value="parameters.label" /] [/#if] > > > [#else] >
> > [#if parameters.label?exists] [@s.property > value="parameters.label" /] [/#if] > > > [/#if] > > The closing template is just: > [#ftl] > > > 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