Colin,
I posed a similar question here, assuming Turbine returned HTML by default. This
assumption was politely corrected. I'm using the Turbine/Velocity combo which seems to
return about any form of common web content easily. With a novice's experience using
these tools, my satisfaction from accomplishing CSV downloads was akin to a caveman
receiving a pack of matches...
Anyway, the particulars (a composite of suggestions in earlier threads here):
I use a Velocity template to lay out the CSV format, for example:
Animal_List.csv, containing a header rec and detail recs:
Status,Name,Reg_No,Bir_Dt,Breeds,Last Calf,Remarks
#foreach ($entry in $thelist.Records)
$entry.Status,$entry.Name,$entry.RegNo,$entry.BirDt,$entry.Breeds,$entry.Remarks
#end
I opted to provide my web developers a pseudo taglib and have created a collection of
classes (registered as session PULL tools) which they can access from within their
template files. In this example, you'll see one such "tag" class, TheList (which is
referenced in the Velocity template above): org.myfirm.modules.taglib.TheList -- which
contains the accessor methods corresponding to the properties above (Status,
Name,etc.).
To send the merged content to the browser, I rely on the following code (which is a
composite of suggestions found elsewhere in messages in this list):
An extended RawScreen:
import java.io.StringWriter;
/**
*
* @version
*/
public class RawContent extends org.apache.turbine.modules.screens.RawScreen {
/** Holds value of property strb_Content. */
private StringBuffer strb_Content;
/** Holds value of property str_Filename. */
private String str_Filename;
/** Creates new RawContent */
public RawContent() {
}
public void doOutput(org.apache.turbine.util.RunData runData) throws
java.lang.Exception {
javax.servlet.http.HttpServletResponse res = runData.getResponse();
res.setContentType("application/binary;name=" + "\"" + str_Filename + "\"");
res.setContentLength(getStrb_Content().length());
res.setHeader("Content-Disposition", "inline; filename=\"" + str_Filename + "\"");
runData.declareDirectResponse();
java.io.PrintWriter out = null;
try {
out = res.getWriter();
out.print(getStrb_Content().toString());
}
catch (java.io.IOException e) {
throw new Exception("RawContent: doOutput: "+e.toString());
}
finally
{ if (out!= null)
{
out.flush();
out.close();
}
}
}
public java.lang.String getContentType(org.apache.turbine.util.RunData runData) {
return "";
}
protected boolean isAuthorized(org.apache.turbine.util.RunData data)
{
// do the security check here. Get whatever info you need
// about the user from RunData
return true;
}
public void setStrb_Content(StringBuffer strb_Content)
{ this.strb_Content = strb_Content; }
public void setStrw_Content(StringWriter strw_Content)
{ setStrb_Content( strw_Content.getBuffer() ); }
public StringBuffer getStrb_Content()
{ return this.strb_Content; }
/** Getter for property str_Filename.
* @return Value of property str_Filename.
*/
public String getStr_Filename() {
return str_Filename;
}
/** Setter for property str_Filename.
* @param str_Filename New value of property str_Filename.
*/
public void setStr_Filename(String str_Filename) {
this.str_Filename = str_Filename;
}
Then from within my Action class, the following method sends the content:
public void doProvideherdlist(RunData data, Context context) throws Exception {
TheList thelObj = (TheList) context.get("thelist");
if ( thelObj.getRecords().size() == 0) {
data.setMessage( "Your herd no longer exists. Please submit a new request.");
setTemplate(data,"the,ReqFormScreen_pg1.vm"); \\ back to first page in the series
return;
}
String results = TurbineVelocity.handleRequest(context,"screens/the/TheDataForm.vm");
RawContent rcSender = new RawContent();
rcSender.setStr_Filename("I" + thelObj.getMemberNum() + ".txt");
rcSender.setStrb_Content(new StringBuffer(results));
rcSender.doOutput( data);
}
Again, this is a composite of suggestions from other threadsin this list... I just
assembled them here. Note that the response headers in the RawContent class were the
ONLY combination that caused the receiving browser to display the "SAVE AS" dialog,
despite content which otherwise woulld have been displayed as text.
HTH
--------------------------------------
Jeff Gehrung (Sr. Programmer Analyst)
Holstein Association USA
Brattleboro VT 05302
802.451.4146
[EMAIL PROTECTED]
[EMAIL PROTECTED]
I'm told
---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now