从jsp文件中的标记库下载文件

时间:2014-03-25 04:17:00

标签: java jsp taglib displaytag

我创建了一个标记库,用于显示与“显示标记”相同的表格以及其他一些功能。现在我想向标签库添加下载选项。我只是通过更改内容类型并重置响应来尝试它,但这是不可能的,因为响应中的内容已经被刷新。      我发现'display tag'使用了一个过滤器,即'ResponseOverrideFilter',以防止发送内容,这样重置响应就可以了。但是我在'web.xml'中找不到任何关于过滤器注册和url-mapping的代码行,因为我使用了'display tag'。请帮我。提前感谢你......

taglibrary:

        <disp:dipxTable id="outer"  style="width:60%; margin-left:20%" list="<%=dtoList %>"  >
             <disp:dipxColumn sortable="true" href="javascript:showReport('${pageUrl }', '${row.monthName }', '${row.year}')" group="1" style="text-align:center;" property="monthName" title="Month"/>
             <disp:dipxColumn group="2" style="text-align:center;" sortable="true"  property="distanceRange" title="Distance"/>
             <disp:dipxColumn   style="text-align:center;" sortable="true"  property="vehicleType" title="Cab"/>
             <disp:dipxColumn style="text-align:center;" sortable="true"  property="totalTrips" expandable="true"  title="#.Trips"/>
             <disp:dipxColumn style="text-align:center;" sortable="true" property="rate" title="Trip Cost"  />
             <disp:dipxColumn style="text-align:center;" sortable="true"  format="{0,number,0.00}"  property="escortRate" title="Escort Cost" /> 
             <div>
             <disp:dipxTable id="inner" type="inner" property="trips" parentProperty="totalTrips" >
                <disp:dipxColumn property="trip_code" title="Trip Code" />
                <disp:dipxColumn property="tripDate"  title="Trip Date" />
                <disp:dipxColumn property="trip_time"  title="Time" />
                <disp:dipxColumn property="trip_log"  title="Shift" />
                <disp:dipxColumn property="tripRate"  title="Trip Rate" />
                <disp:dipxColumn property="escortRate"  title="Escort Cost" />

             </disp:dipxTable>
             </div>
              <disp:DispxGroupSummaryRow groupProperty="monthName" id="grp">

                <td></td>
                <td></td>
                 <td></td>
                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="totalTrips" />
                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="rate"  />
                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="escortRate" format="{0,number,0.00}" />
             </disp:DispxGroupSummaryRow>
             <disp:DispxSummaryRow style="background-color:#fff;" >

                <td colspan="3"><center>Grand Total </center></td>

                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="totalTrips" />
                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="rate"  />
                 <disp:dipxColumn style="text-align:center; font-weight:bold;"  property="escortRate"  format="{0,number,0.00}"  />

             </disp:DispxSummaryRow>

        </disp:dipxTable>

tld文件内容标记dispxTable

<tag>
        <description>This tag is parent tag to display table </description>
        <name>dipxTable</name>
        <tag-class>com.nfl.table.DispxTable</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>list</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>type</name>
             <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>property</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>parentProperty</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>id</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>style</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue> 
        </attribute>
        <attribute>
            <name>styleClass</name>
            <required>false</required>
             <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>headerRowStyleClass</name>
            <required>false</required>
             <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>headerRowStyle</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue> 
        </attribute>
        <attribute>
            <name>rowStyleClass</name>
            <required>false</required>
             <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>rowStyle</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue> 
        </attribute>
        <attribute>
            <name>expanded</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue> 
        </attribute>

</tag>

代码支持类

enter code here

package com.nfl.table;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import com.nfl.dto.SorterFood;
import com.nfl.dto.support.NflJSONObject;
import com.nfl.dto.support.Sorter;
import com.nfl.dto.support.exception.NflJSONException;
import com.nfl.table.container.ColumnContainer;

public class DispxTable  extends SimpleTagSupport implements ColumnContainer {
/* type of table , inner or outer */
private String type;

/* property name of object to be displayed */
private String property;

/* property name of parent table where this table is drawn as child table of the given property */
private String parentProperty;

/* class attribute of table */
private String styleClass;

/* style attribute of table */
private String style;

/* style attribute of th within thead */
private String headerRowStyle;

/* style attribute of tr within thead */
private String headerRowStyleClass;

/* style attribute of tr within tbody */
private String rowStyle;

/* class attribute of tr within tbody */
private String rowStyleClass;

/* this is a property of inner table to render as expanded by default */
private boolean expanded;

/* id attribute of table */
private String id;

private int colomns;
private boolean groupPropegation;


public String getHeaderRowStyle() {
    return headerRowStyle;
}
public void setHeaderRowStyle(String headerRowStyle) {
    this.headerRowStyle = headerRowStyle;
}
public String getHeaderRowStyleClass() {
    return headerRowStyleClass;
}
public void setHeaderRowStyleClass(String headerRowStyleClass) {
    this.headerRowStyleClass = headerRowStyleClass;
}
public String getRowStyle() {
    return rowStyle;
}
public void setRowStyle(String rowStyle) {
    this.rowStyle = rowStyle;
}
public String getRowStyleClass() {
    return rowStyleClass;
}
public void setRowStyleClass(String rowStyleClass) {
    this.rowStyleClass = rowStyleClass;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
private Object row;

/* each dto is kept as NflJSONObject as a generic */
private NflJSONObject summaryItem;

/* keeps summary attributes */
private Map<String, NflJSONObject> groupSummaryMap = new HashMap<String, NflJSONObject>(); 

public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String getProperty() {
    return property;
}
public void setProperty(String property) {
    this.property = property;
}
private int level;
private Object innerList;
private Object list;
private   NflJSONObject item;
private   NflJSONObject preItem;
private   NflJSONObject nextItem;
public NflJSONObject getPreItem() {
    return preItem;
}
public void setPreItem(NflJSONObject preItem) {
    this.preItem = preItem;
}
private String name="sddfd";
private boolean isHeader;
private int rowCount;
private  HashMap<Integer, SorterFood> groupMap= new HashMap<Integer, SorterFood>();
private HashMap<Integer, SorterFood> sortMap= new HashMap<Integer, SorterFood>();

private HashMap<Integer, SorterFood>   putAll(HashMap<Integer, SorterFood> pgMap,HashMap<Integer, SorterFood> psMap) {
    HashMap<Integer, SorterFood> allMap=new HashMap<Integer, SorterFood>();

    Set<Integer> keys=null;
    int i=0;
    if(pgMap!=null&&pgMap.size()>0) {
        keys= pgMap.keySet();

        for(int key:keys) {
            allMap.put(key, pgMap.get(key));
            //System.out.println("pg keys " + key+" i:"+i);
            //System.out.println("putting ("+key+", "+" "+psMap.get(key)+")");
            if(key>i) {
                i=key;
            }
        }
    }
    if(psMap!=null&&psMap.size()>0) {
        keys= psMap.keySet();

        for(int key:keys) {


            Set <Integer> gkeys=pgMap.keySet();
            boolean contains=false;
            for(int gkey:gkeys) {
                SorterFood gSf=pgMap.get(gkey);
                SorterFood sSf= psMap.get(key);
                if(gSf.getProperty().equals(sSf.getProperty())) {
                    contains=true;


                    allMap.put(gkey,sSf);

                    break;
                }

            }
            if(!contains) {
                allMap.put(i+1, psMap.get(key));
            }


         i++;
        }




    }

    return allMap;

}

public NflJSONObject getItem() {
    return item;
}
public void setItem(NflJSONObject item) {
    this.item = item;
}
public HashMap<Integer, SorterFood> getGroupMap() {
    return groupMap;
}
public void setGroupMap(HashMap<Integer, SorterFood> groupMap) {
    this.groupMap = groupMap;
}
public HashMap<Integer, SorterFood> getSortMap() {
    return sortMap;
}
public void setSortMap(HashMap<Integer, SorterFood> sortMap) {
    this.sortMap = sortMap;
}
public Object  getList() {
    return list;
}
public void setList(Object  list) {
    this.list = list;
}




private String returnClass() {
    String styleString="";
    if(styleClass==null||styleClass.trim().equals("") ) {

         styleString="class=\"displaytag\"";
    }else {
        // System.out.println("=================================class=="+styleClass);
         styleString="class=\""+ styleClass+"\"";   
    }

    return styleString;

}


private String returnHeaderClass() {
    String styleString="";
    if(headerRowStyleClass ==null||headerRowStyleClass.trim().equals("") ) {

         styleString="";
    }else {
        //    System.out.println("=================================class=="+styleClass);
         styleString="class=\""+ headerRowStyleClass+"\"";  
    }

    return styleString;

}

private String returnHeaderStyle() {
    String styleString="";
    if(headerRowStyle!=null&&headerRowStyle.trim().equals("")==false ) {


         styleString=" style=\""+ headerRowStyle+"\" "; 
    }

    return styleString;

}

private String returnRowClass(int index) {
    String styleString="";
    String commonClass= ((index%2==0)?"even":"odd") ;
    if(rowStyleClass ==null||rowStyleClass.trim().equals("") ) {

         styleString= "class=\"" + commonClass+ "\"";


    }else {
        // System.out.println("=================================class=="+styleClass);
         styleString="class=\"" + commonClass + " "+ rowStyleClass+"\"";    
    }

    return styleString;

}

private String returnRowStyle() {
    String styleString="";
    if(rowStyle!=null&&rowStyle.trim().equals("")==false ) {


         styleString=" style=\""+ rowStyle+"\" ";   
    }

    return styleString;

}


private String returnStyle() {
    String styleString="";
    if(style!=null&&style.trim().equals("")==false ) {


         styleString=" style=\""+ style+"\" ";  
    }

    return styleString;

}


@Override
public void doTag() throws JspException, IOException {
    // TODO Auto-generated method stub


     PageContext ctx = (PageContext)getJspContext();

     HttpServletRequest request = (HttpServletRequest) ctx.getRequest();
     HttpServletResponse response = (HttpServletResponse) ctx.getResponse();

     String download=request.getParameter("download");
     if(download!=null&&download.equals("xls")) {
         String mimeType = "application/vnd.ms-excel";
         //response.flushBuffer();

    //  response.reset();
     //    ctx.getOut().clearBuffer();
     //  response.flushBuffer();



        /* for(String s:response.getHeaderNames()) {
             // System.out.println("Header "+s + " value "+response.getHeader(s));
         }*/
         response.setContentType(mimeType);        
         String fname = new Date().toString();
         response.setHeader("Content-Disposition", "inline; filename = APL" + fname + ".xls");        
         response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
         response.setHeader("Cache-Control", "post-check=0, pre-check=0");
         response.setHeader("Pragma", "no-cache");
         response.setHeader("Pragma", "public");
         response.setHeader("Expires", "Mon, 1 Jan 1995 05:00:00 GMT");
         Boolean b = (Boolean) ctx.getAttribute("pageStarted");
/*         if(b==null||b==false) {
         ctx.getOut().write("<html><head></head><body><form>");
         ctx.setAttribute("pageStarted", true);
         }*/

     }



    JspWriter out = getJspContext().getOut();

    List list = (List) getList();
    preItem=null;
    if(type!=null&&type.equals("inner"))
    {

        /*
         * inner table starts
         */

        DispxTable t= (DispxTable) getParent();
         int myRowCount=t.getRowCount();
         if(!t.isHeader())
         {

             if(t!=null)
                {
                 // System.out.println("inner table row ");
                 //org.json.JSONObject obj= new org.json.JSONObject(t.getItem());
                 NflJSONObject obj;

                 /*catch (JSONException e) {
                    // TODO Auto-generated catch block
                //  e.printStackTrace();
                    // System.out.println(""+e);
                    out.write("</tr><tr><td>"+e+"</td></tr><tr>");
                }*/

                  //// System.out.println("Param "+  request.getParameter("expandableProperty"));
                    String paramRowCount=request.getParameter(t.getId()+"-rowCount");
                       String exandablePropery=request.getParameter(t.getId()+"-expandableProperty");
                      parentProperty= parentProperty==null?"":parentProperty;
                   exandablePropery= exandablePropery==null?"":exandablePropery;

                     if((parentProperty.equals("")||exandablePropery.equals("")) ==false&& parentProperty.equals(exandablePropery)&&String.valueOf(myRowCount).equals(paramRowCount) || isExpanded())
                    {
                     try {
                         obj= t.getItem();
                        level=t.getLevel()+1;   
            //              // System.out.println("subjects : "+obj.get(property));  
                        Object object= obj.get(property);
                        list = (List) object;



                        if(list!=null&&list.size()>0)
                        {

                            setList(list);
                            out.write(" </tr> ");
                            out.write("<tr><td colspan='" + t.getColomns() + "' >");
                            String tableStart=String.format("<a onClick=\"closeMyParent('%s')\" >Close</a><br/><table %s %s %s> <thead> <tr %s %s >",id,returnStyle(), returnClass(),"id=\""+id+"\"", returnHeaderClass(), returnHeaderStyle() );
                            isHeader=true;
                            out.write(tableStart);
                            row=list.get(0);
                            getJspBody().getJspContext().setAttribute("row", row);


                            getJspBody().invoke(out);

                            out.write(" </tr> </thead> <tbody>");
                            isHeader=false;
                            Map<Integer,SorterFood> allMap= putAll(getGroupMap(), getSortMap());
                            //System.out.println("Sort map size :"+ sortMap.size());
                            //System.out.println("All map size :"+ allMap.size());
                            if(allMap!=null&&allMap.size()>0) {
                                list=Sorter.sort(list, allMap);
                            }
                            rowCount=0;
                            for(Object o:list){
                                setGroupPropagation(true);
                                setColomns(0);
                                row=o;

                                item=new NflJSONObject( row);
                                if(list.indexOf(o)<(list.size()-1)) {
                                    nextItem = new NflJSONObject( list.get(list.indexOf(o)+1));
                                } else {
                                    nextItem = new NflJSONObject();
                                }
                                getJspBody().getJspContext().setAttribute("row", row);
                                getJspBody().getJspContext().setAttribute("rowCount",list.indexOf(row));
                                out.write(String.format(" <tr %s %s > ", returnRowClass(list.indexOf(row)), returnRowStyle()) );
                                getJspBody().invoke(null);
                                preItem=item;
                                out.write("</tr>");
                                rowCount++;
                            }
                            out.write(" </tbody> </table></div>");
                            out.write(" </td></tr> ");
                            out.write("<tr style='display:none;'> <td colspan='" + t.getColomns() + "'> </td> ");
                        } 





                    } catch (  NflJSONException e) {
                        // TODO Auto-generated catch block
                        throw new IOException(""+e);
                    }



                    }

                }

         }




        /*
         * inner table ends
         */



    } else if(list!=null&&list.size()>0)
    {

        String tableStart=String.format("<table %s %s %s > <thead> <tr %s %s >",returnStyle(), returnClass(),"id=\""+id+"\" ", returnHeaderClass(), returnHeaderStyle() );
        isHeader=true;
        out.write(tableStart);
        row=list.get(0);
        getJspBody().getJspContext().setAttribute("row", row);

        getJspBody().invoke(null);

        out.write("</tr> </thead> <tbody>");
        isHeader=false;
        Map<Integer,SorterFood> allMap= putAll(getGroupMap(), getSortMap());
        //System.out.println("Group map size >"+ getGroupMap().size());
        //System.out.println("Sort map size >"+ getSortMap().size());
        //System.out.println("All map size >"+ allMap.size());
        if(allMap!=null&&allMap.size()>0) {
            list=Sorter.sort(list, allMap);
        }

        rowCount=0;
        for(Object o:list){
            setGroupPropagation(true);
            setColomns(0);
            row=o;
            // System.out.println("outer table row open");

            try {
                item=new NflJSONObject( o);
                if(list.indexOf(o)<(list.size()-1)) {
                    nextItem = new NflJSONObject( list.get(list.indexOf(o)+1));
                } else {
                    nextItem = new NflJSONObject();
                }
            } catch (NflJSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //ctx.setAttribute("row", item);

            getJspBody().getJspContext().setAttribute("row", row);
            getJspBody().getJspContext().setAttribute("rowCount",list.indexOf(row));
            out.write(String.format(" <tr %s %s > ", returnRowClass(list.indexOf(row)), returnRowStyle() ) ); 
            getJspBody().invoke(out);
            preItem=item;
            out.write(" </tr> ");
            // System.out.println("outer table row close");
            rowCount++;
        }
        out.write("</tbody> </table>");
    }else
    {
        out.write("<div> No display </div>");
    }

/*  if(download!=null&&download.equals("xls")) {
        Boolean b = (Boolean) ctx.getAttribute("pageStarted");
        if(b!=null&&b==true) {

            out.write("</form></body></html>" );
        ctx.setAttribute("pageStarted", true);
        }

    }
    */


}
public boolean isHeader() {
    return isHeader;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Object getInnerList() {
    return innerList;
}
public String getParentProperty() {
    return parentProperty;
}
public void setParentProperty(String parentProperty) {
    this.parentProperty = parentProperty;
}
public int getRowCount() {
    return rowCount;
}
public void setRowCount(int rowCount) {
    this.rowCount=rowCount;
}
public int getColomns() {
    return colomns;
}
public void setColomns(int colomns) {
    this.colomns = colomns;
}
public String getStyleClass() {
    return styleClass;
}
public void setStyleClass(String styleClass) {
    this.styleClass = styleClass;
}
public NflJSONObject getSummaryItem() {
    return summaryItem;
}
public void setSummaryItem(NflJSONObject summaryItem) {
    this.summaryItem = summaryItem;
}
public Object getRow() {
    return row;
}
public void setRow(Object row) {
    this.row = row;
}
public int getLevel() {
    return level;
}
public String getStyle() {
    return style;
}
public void setStyle(String style) {
    this.style = style;
}
@Override
public void setGroupPropagation(boolean flag) {
    // TODO Auto-generated method stub
    groupPropegation = flag;


}
@Override
public boolean hasGroupPropagation() {
    // TODO Auto-generated method stub
    return groupPropegation;
}
public NflJSONObject getNextItem() {
    return nextItem;
}
public void setNextItem(NflJSONObject nextItem) {
    this.nextItem = nextItem;
}
public Map<String, NflJSONObject> getGroupSummaryMap() {
    return groupSummaryMap;
}
public boolean isExpanded() {
    return expanded;
}
public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}

}

我认为你不需要所有这些细节来回答如何只注册taglibary中定义的过滤器。

0 个答案:

没有答案