Discussion:
How to output a tab from a velocity template?
Bryce Nesbitt
2005-12-15 00:38:09 UTC
Permalink
I'm using a velocity/xwork/WebWork template to write a text file, which
needs to have tabs between each record:

$res.setHeader("content-type", "text/plain")#*
*#$res.setHeader("Content-Disposition", "attachment; filename=xxxxx.tsv")
#foreach( $step in $result )
RECORD&tab;$!step.PrimaryContact.firstName&tab;$!step.PrimaryContact.lastName
#end

How can I get tabs or other special characters out of Velocity? \t does
not work. &tab; and 	 won't work, because the target is not a HTML
browser.

Putting actual tabs in the text file might work, but that's icky.
Will Glass-Husain
2005-12-15 01:20:25 UTC
Permalink
Define an reference $tab in the context?

context.put("tab","\t");

WILL

----- Original Message -----
From: "Bryce Nesbitt" <***@obviously.com>
To: <velocity-***@jakarta.apache.org>
Sent: Wednesday, December 14, 2005 4:38 PM
Subject: How to output a tab from a velocity template?
Post by Bryce Nesbitt
I'm using a velocity/xwork/WebWork template to write a text file, which
$res.setHeader("content-type", "text/plain")#*
*#$res.setHeader("Content-Disposition", "attachment; filename=xxxxx.tsv")
#foreach( $step in $result )
RECORD&tab;$!step.PrimaryContact.firstName&tab;$!step.PrimaryContact.lastName
#end
How can I get tabs or other special characters out of Velocity? \t does
not work. &tab; and &#09; won't work, because the target is not a HTML
browser.
Putting actual tabs in the text file might work, but that's icky.
---------------------------------------------------------------------
Nathan Bubna
2005-12-15 01:22:41 UTC
Permalink
Post by Bryce Nesbitt
I'm using a velocity/xwork/WebWork template to write a text file, which
$res.setHeader("content-type", "text/plain")#*
*#$res.setHeader("Content-Disposition", "attachment; filename=xxxxx.tsv")
#foreach( $step in $result )
RECORD&tab;$!step.PrimaryContact.firstName&tab;$!step.PrimaryContact.lastName
#end
How can I get tabs or other special characters out of Velocity? \t does
not work. &tab; and &#09; won't work, because the target is not a HTML
browser.
Putting actual tabs in the text file might work, but that's icky.
if actual tabs aren't icky in the output, then i don't see why they're
icky in the input. Velocity doesn't do anything special for or with
tab characters. if you want a tab character in the output, the
simplest thing is to put it in the input. but if you really don't
like that, you can always put a string containing just a tab in the
context:

context.put("tab", "\t");

and then use

${tab} in your template.
Post by Bryce Nesbitt
---------------------------------------------------------------------
Bryce Nesbitt
2005-12-15 02:08:39 UTC
Permalink
Post by Nathan Bubna
if actual tabs aren't icky in the output, then i don't see why they're
icky in the input. Velocity doesn't do anything special for or with
tab characters.
Because the source code is in a multiplatform multideveloper environment
that does not preserve (or allow,
or tolerate) tabs.
The export is in user-land.
Post by Nathan Bubna
if you want a tab character in the output, the
simplest thing is to put it in the input. but if you really don't
like that, you can always put a string containing just a tab in the
context.put("tab", "\t");
and then use
${tab} in your template.
Cool, could you give me one more hint on where to find the right
context? I did this, without joy:


// Action.java
...extends ActionSupport...

public String execute() throws Exception {
ActionContext.getContext().put("tab","\t");
return "tabexport";
}


<!-- something.vm -->
$res.setHeader("content-type", "text/plain")
$res.setHeader("Content-Disposition", "attachment;
filename=cust_-xxxxxx.txt")TFLD4
TEST$tab${tab}TEST
TEST$tab${tab}TEST
Nathan Bubna
2005-12-15 03:25:43 UTC
Permalink
Post by Bryce Nesbitt
Post by Nathan Bubna
if actual tabs aren't icky in the output, then i don't see why they're
icky in the input. Velocity doesn't do anything special for or with
tab characters.
Because the source code is in a multiplatform multideveloper environment
that does not preserve (or allow,
or tolerate) tabs.
The export is in user-land.
ah, that makes sense.
Post by Bryce Nesbitt
Post by Nathan Bubna
if you want a tab character in the output, the
simplest thing is to put it in the input. but if you really don't
like that, you can always put a string containing just a tab in the
context.put("tab", "\t");
and then use
${tab} in your template.
Cool, could you give me one more hint on where to find the right
looks like some sort of WebWork context. you want to get it into the
Velocity context. i don't know much about WebWork's Velocity
integration...
Post by Bryce Nesbitt
// Action.java
...extends ActionSupport...
public String execute() throws Exception {
ActionContext.getContext().put("tab","\t");
return "tabexport";
}
<!-- something.vm -->
$res.setHeader("content-type", "text/plain")
$res.setHeader("Content-Disposition", "attachment;
filename=cust_-xxxxxx.txt")TFLD4
TEST$tab${tab}TEST
TEST$tab${tab}TEST
if the response is available as $res, perhaps the request is available
as $req (old VelocityServlet style). if so, then at worst you could
do:

request.setAttribute("tab", "\t")

and

#set( $tab = $req.getAttribute('tab') )
TEST${tab}${tab}TEST
Post by Bryce Nesbitt
---------------------------------------------------------------------
Bryce Nesbitt
2005-12-16 05:38:40 UTC
Permalink
Based on the kind help I got on this list, here's my final solution.
I added the following method to the matching action:

public String getTab() {
return "\t";
}

Then simply used ${tab} in the velocity page:
CUST${tab}NAME${tab}BADDR1${tab}BADDR2${tab}BADDR3

This may be specific to webwork/xwork.
Rakesh Reddy
2005-12-15 15:41:04 UTC
Permalink
Hi Group --

I am new to use the velociity framework. How do we Internationalize the textx in a velocity template ?

For example, if I have the following template.

###############################
The following students must be required to attend the Gelogy Class:
#foreach ($name in $list)
$name should attend Geology
end
###############################

Now in the above example how do I make the text "The following students must be required to attend the Gelogy Class:" a label.

label.student.instruction=The following students must be required to attend the Gelogy Class:

How I do use this label in the velocity template ? To do this do we need to do any special processing in the velocity template processor.

Thanks
Rakesh




---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping
Carfield Yim
2005-12-15 16:54:11 UTC
Permalink
For me, just simple press the resourcebundle as the parameter
Post by Rakesh Reddy
Hi Group --
I am new to use the velociity framework. How do we Internationalize the textx in a velocity template ?
For example, if I have the following template.
###############################
#foreach ($name in $list)
$name should attend Geology
end
###############################
Now in the above example how do I make the text "The following students must be required to attend the Gelogy Class:" a label.
How I do use this label in the velocity template ? To do this do we need to do any special processing in the velocity template processor.
Thanks
Rakesh
---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping
Bryce Nesbitt
2005-12-16 05:18:31 UTC
Permalink
I have a fairly simple velocity page which I have inherited.

When loaded, on some platforms and browsers, it mostly 'works' but never
'finishes' loading.
Turns out the HTTP headers claim the file is 31,940 bytes, and the
actual file (as loaded)
is only 19,000. So any client that looks at the length hangs. Others
get it instantly.

The returned HTTP headers are:
----------------------------------
HTTP/1.1 200 OK
Date: Fri, 16 Dec 2005 05:11:12 GMT
Server: Apache/1.3.29 (Unix) Resin/2.1.12 mod_ssl/2.8.16 OpenSSL/0.9.6l
Cache-Control: max-age=86400
Expires: Sat, 17 Dec 2005 05:11:12 GMT
Last-Modified: Wed, 14 Dec 2005 20:02:07 GMT
Content-Length: 31940
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html

The file is no more complex than:
-----------------------------------
#macro( errorMessageLocal $message )
#if( $fieldErrors.size() > 0 )
<tr><td>
<div style="border: 1px solid #f00; background-color: #fdd;
font-size:9pt;">
#if( $message ) <strong>$!{message}</strong> #end
<ul style="font-size:9pt;">
#foreach( $error in $fieldErrors )
<li>$error</li>
#end
</ul>
</div>
</td></tr>
#end
#end

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Individual Member Application</title>
<link rel="stylesheet" type="text/css" href="application_style.css" />
</head>
<body>
....
#errorMessageLocal( "Please fix the following errors:" )
...


Has anyone seen things like this before? Any clues before I brute force
binary search the dang thing?
Bryce Nesbitt
2005-12-16 19:56:02 UTC
Permalink
The following very simple .html file is enough to trigger the issue I'm
seeing:

-----------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

#macro( screwthingsupmacro $values )
<p>I cause the Content-Length: header to be wrong!</p>
#end

<html>
<head>
<title>Individual Member Application</title>
</head>
<body>
<form action="signup.action" method="POST">
</form>
</body>
</html>
-----------------------------------------------

host> wget --save-headers http://localhost:8080/exploded/signup/e_test.html

HTTP request sent, awaiting response... 200 OK
Length: 350 [text/html]
71% [=========================> ] 249 --.--K/s
ETA 00:02
Connection closed at byte 249. Retrying.

Loading...