velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Will Glass-Husain" <wgl...@forio.com>
Subject Re: Setting contentType in a Velocity Template [was Re: How do I pre-populat text, not fields, in a form?(Making forms dynamic)]
Date Thu, 07 Jul 2005 18:41:12 GMT
You need to do this in the controller.  Velocity does not automatically have 
the response object in the context - only if it's passed in.

I've struggled with this in the Spring MVC.  (I haven't used the Web Flow). 
Frustratingly, the VelocityViewResolver sets the content type resulting in 
that if you set it yourself in the response it's overwritten.  I handled 
this by creating a special resolver that only applied to a specified 
directory.  (LimitedViewResolver).  Then I set the content type within the 
resolver.

I also used a servlet filter to add the following response header:

httpresponse.addHeader("Content-disposition","attachment; 
filename=export.xls");

which forces the Open/Save dialog.  I couldn't set the content type in the 
filter because (as noted above) this is overridden by the ViewResolver.

Maybe there's an easier way.  Let me know what you end up doing.  Below is 
my "dispatcher-servlet.xml" and the source to the custom ViewResolver.

WILL


    <!-- view resolver for HTML files with Excel content type -->
    <!-- only applies to views within specified directory -->
 <bean id="excelViewResolver"
   class="com.forio.sub.view.LimitedVelocityViewResolver">
     <property name="order"><value>0</value></property>
       <property name="cache"><value>true</value></property>
       <property name="prefix"><value></value></property>
       <property name="suffix"><value></value></property>
    <property name="exposeSpringMacroHelpers"><value>true</value></property>
    <property name="exposeRequestAttributes"><value>true</value></property>
    <property name="requestContextAttribute"><value>rc</value></property>

         <property 
name="filterDirectory"><value>/secure/spreadsheets</value></property>
       <property 
name="contentType"><value>application/vnd.ms-excel</value></property>
       <property name="forceDownload"><value>true</value></property>
 </bean>

    <!-- basic view resolver for HTML files -->
    <bean
      id="viewResolver"
      class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
   <property name="exposeSpringMacroHelpers"><value>true</value></property>
   <property name="exposeRequestAttributes"><value>true</value></property>
   <property name="requestContextAttribute"><value>rc</value></property>

      <property name="cache"><value>true</value></property>
      <property name="prefix"><value></value></property>
      <property name="suffix"><value></value></property>
    </bean>


package com.forio.sub.view;

import org.springframework.core.Ordered;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;

import java.util.Locale;


/**
 * A VelocityViewResolver that only runs on view names within a particular 
directory.
 * Use this to chain multiple resolvers together.
 */
public class LimitedVelocityViewResolver extends VelocityViewResolver
    implements Ordered {
    int order;
    boolean forceDownload;
    private String filterDirectory;

    /**
     * Only return views for specific view names
     */
    public View resolveViewName(String viewName, Locale locale)
        throws Exception {
        if (viewName.startsWith(filterDirectory)) {
            View view = super.resolveViewName(viewName, locale);

            return new DownloadView(view);
        } else {
            return null;
        }
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public String getFilterDirectory() {
        return filterDirectory;
    }

    public void setFilterDirectory(String filterDirectory) {
        filterDirectory = filterDirectory.trim();

        if (!filterDirectory.startsWith("/")) {
            filterDirectory = "/" + filterDirectory;
        }

        if (!filterDirectory.endsWith("/")) {
            filterDirectory = filterDirectory + "/";
        }

        this.filterDirectory = filterDirectory;
    }

    public boolean isForceDownload() {
        return forceDownload;
    }

    public void setForceDownload(boolean forceAttachment) {
        this.forceDownload = forceAttachment;
    }
}


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


Mime
View raw message