You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dan Allen <da...@mojavelinux.com> on 2003/03/15 22:09:24 UTC

nl2br taglib

Are there any taglibs that have an attribute similar to
filter="true" for the <bean:write> that would do nl2br="true"

Users enter non-html text and when I display it I want to replace
endline characters with html line break markups so that it preserves
the whitespace.

If not, I guess I will be learning how to do a custom taglib today.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Windows: where do you want your data to disappear to today?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: nl2br taglib

Posted by Arron Bates <st...@keyboardmonkey.com>.
Dan,

I think that you may have better luck in a tag that doesn't actually extend or
particularly work around the write tag, but simply filters the results of body
content. That way your not just restricted to filering the content of a bean
property. Markup would be like...


<mylib:nl2br> whatever <nested:write property="something" /></mylib:nl2br>


...And in this would could get the advantages of nested tags or whatever form
you wanted. It also means you don't have to worry about blocking/supporting
the logic of any underlying tag.


The tag class itself would look like...

// import whatever

public class NL2BR extends BodyTagSupport {
  
  public int doAfterBody() throws JspException {
    if (bodyContent != null) {
      String filteredContent = bodyContent.getString();
      
      // filter here...

      ResponseUtils.writePrevious(pageContext, filteredContent);
      bodyContent.clearBody();
    }
  }
}


...this should, in theory, do the trick. No properties or anything, simply
filter whatever comes through in the body content.


Arron.


PS: get my mail the other day?...
  email client plays funny buggers at times,
  so I don't know what's sent and what's not.



> ...see, I'm fading here...trial #2
> 
> Dan
> 
> [WriteTag.java]
> 
> package net.creativerge.struts.taglib.bean;
> 
> import javax.servlet.jsp.JspException;
> 
> import org.apache.struts.util.RequestUtils;
> import org.apache.struts.util.ResponseUtils;
> 
> public class WriteTag extends org.apache.struts.taglib.bean.WriteTag
> {
> 	protected boolean nl2br = false;
> 
> 	public boolean getNl2br()
> 	{
> 		return nl2br;
> 	}
> 
> 	public void setNl2br(boolean nl2br)
> 	{
> 		this.nl2br = nl2br;
> 	}
> 
> 	public int doStartTag() throws JspException
> 	{
> 		// Look up the requested bean and skip if ignoring and it does not 
> exist 		if (ignore && (RequestUtils.lookup(pageContext, name, scope) 
> == null)) 		{ 			return (SKIP_BODY); 		}
> 
> 		// Look up the requested property value
> 		Object value =	RequestUtils.lookup(pageContext, name, property,
>  scope); 		if (value == null) 		{ 			return (SKIP_BODY); 		}
> 
> 		// Convert value to the String with some formatting
> 		String output = formatValue(value);
> 
> 		// filter xml characters if requested
> 		if (filter)
> 		{
> 			output = ResponseUtils.filter(output);
> 		}
> 
> 		// convert endlines to <br /> tags if requested
> 		if (nl2br)
> 		{
> 			output = output.replaceAll("\n", "<br />");
> 		}
> 
> 		// Print this property value to our output writer
> 		ResponseUtils.write(pageContext, output);
> 
> 		// Continue processing this page
> 		return (SKIP_BODY);
> 	}
> }
> 
> -- 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> Daniel Allen, <da...@mojavelinux.com>
> http://www.mojavelinux.com/
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> There is no such thing as a casual knowledge of xslt, either 
> you know everything about it or you are sitting in the creek.
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org




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


Re: nl2br taglib

Posted by Dan Allen <da...@mojavelinux.com>.
...see, I'm fading here...trial #2

Dan

[WriteTag.java]

package net.creativerge.struts.taglib.bean;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

public class WriteTag extends org.apache.struts.taglib.bean.WriteTag
{
	protected boolean nl2br = false;

	public boolean getNl2br()
	{
		return nl2br;
	}

	public void setNl2br(boolean nl2br)
	{
		this.nl2br = nl2br;
	}

	public int doStartTag() throws JspException
	{
		// Look up the requested bean and skip if ignoring and it does not exist
		if (ignore && (RequestUtils.lookup(pageContext, name, scope) == null))
		{
			return (SKIP_BODY);
		}

		// Look up the requested property value
		Object value =	RequestUtils.lookup(pageContext, name, property, scope);
		if (value == null)
		{
			return (SKIP_BODY);
		}

		// Convert value to the String with some formatting
		String output = formatValue(value);

		// filter xml characters if requested
		if (filter)
		{
			output = ResponseUtils.filter(output);
		}

		// convert endlines to <br /> tags if requested
		if (nl2br)
		{
			output = output.replaceAll("\n", "<br />");
		}

		// Print this property value to our output writer
		ResponseUtils.write(pageContext, output);

		// Continue processing this page
		return (SKIP_BODY);
	}
}

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
There is no such thing as a casual knowledge of xslt, either 
you know everything about it or you are sitting in the creek.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: nl2br taglib

Posted by Dan Allen <da...@mojavelinux.com>.
Attached is a hacked version of WriteTag.java from the struts
library to include a nl2br attribute which will convert "\n" to "<br
/>" in the output.  It was a pretty simple hack and you can just
update your .tld file to include the java class
net.creativerge.struts.taglib.bean.WriteTag and change your .tld
definition to include the nl2br attribute (just like the filter
attribute) and you will be there.

Someone can feel free to add this to bugzilla, but I have way too
much on my plate right now to put up a fight.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"This is a test of the Emergency Broadcast System.  If this had 
been an actual emergency, do you really think we'd stick around 
to tell you?"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: nl2br taglib

Posted by Dan Allen <da...@mojavelinux.com>.
Anil Amarakoon (awc@drytel.net) wrote:

> No such thing exists. May be you are already doing this on jsp. here we
> go..
> <%
>      myString.replace("\n","<br>");
>     pageContext.setAttribute(""anyName",myString);
> %>
> 
> <bean:write name="anyName" filter="false">
> 
> .anil

Fine, I'll write a custom taglib, because there is absolutely no
reason that this should be a scriplet.  This is exactly equivalence
in importance in tablib terms as html tag escaping.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"I'm old enough to know better, but still too young to care."
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: nl2br taglib

Posted by Anil Amarakoon <aw...@drytel.net>.
No such thing exists. May be you are already doing this on jsp. here we
go..
<%
     myString.replace("\n","<br>");
    pageContext.setAttribute(""anyName",myString);
%>

<bean:write name="anyName" filter="false">

.anil


Dan Allen wrote:

> Are there any taglibs that have an attribute similar to
> filter="true" for the <bean:write> that would do nl2br="true"
>
> Users enter non-html text and when I display it I want to replace
> endline characters with html line break markups so that it preserves
> the whitespace.
>
> If not, I guess I will be learning how to do a custom taglib today.
>
> Dan
>
> --
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> Daniel Allen, <da...@mojavelinux.com>
> http://www.mojavelinux.com/
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> Windows: where do you want your data to disappear to today?
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


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