|
| 1 | +package com.aspose.gridweb.test; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | +import java.lang.reflect.Method; |
| 8 | + |
| 9 | +import javax.servlet.http.HttpServlet; |
| 10 | +import javax.servlet.http.HttpServletRequest; |
| 11 | +import javax.servlet.http.HttpServletResponse; |
| 12 | + |
| 13 | +import com.aspose.gridweb.ExtPage; |
| 14 | +import com.aspose.gridweb.GridWebBean; |
| 15 | +import com.aspose.gridweb.Unit; |
| 16 | +import com.aspose.gridweb.test.util.FileUtil; |
| 17 | + |
| 18 | +public abstract class TestGridWebBaseServlet extends HttpServlet { |
| 19 | + private static final long serialVersionUID = 1L; |
| 20 | + protected ExtPage page =ExtPage.getInstance(); |
| 21 | + protected PrintWriter out = null; |
| 22 | + protected String path = null; |
| 23 | + protected String webPath = null; |
| 24 | + |
| 25 | + |
| 26 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) { |
| 27 | + |
| 28 | + doPost(request, response); |
| 29 | + } |
| 30 | + |
| 31 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
| 32 | + |
| 33 | + GridWebBean gridweb=page.getBean(request); |
| 34 | + //we shall call it to update request and response in gridweb before render |
| 35 | + gridweb.setReqRes(request, response); |
| 36 | + try { |
| 37 | + request.setCharacterEncoding("UTF-8"); |
| 38 | + } catch (UnsupportedEncodingException e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + response.setCharacterEncoding("UTF-8"); |
| 42 | + path = request.getServletContext().getRealPath("/"); |
| 43 | + webPath = request.getServletContext().getContextPath(); |
| 44 | + |
| 45 | + try { |
| 46 | + out = response.getWriter(); |
| 47 | + |
| 48 | + |
| 49 | + // do the reflect method |
| 50 | + this.process(gridweb,request, response); |
| 51 | + |
| 52 | + gridweb.prepareRender(); |
| 53 | + String html = gridweb.getHTMLBody(); |
| 54 | + out.print(html); |
| 55 | +// FileUtil.putFile(html); |
| 56 | + |
| 57 | + out.flush(); |
| 58 | + |
| 59 | + } catch (IOException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + }finally{ |
| 62 | + out.close(); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + public void process(GridWebBean gridweb,HttpServletRequest request, HttpServletResponse response) { |
| 69 | + String action = request.getParameter("flag"); |
| 70 | + if (action == null) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("rawtypes") |
| 75 | + Class clz = this.getClass(); |
| 76 | + Method method = null; |
| 77 | + try { |
| 78 | + method = clz.getDeclaredMethod(action,GridWebBean.class, HttpServletRequest.class, HttpServletResponse.class); |
| 79 | + method.invoke(this,gridweb, request, response); |
| 80 | + } catch (SecurityException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } catch (NoSuchMethodException e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } catch (IllegalArgumentException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } catch (IllegalAccessException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + } catch (InvocationTargetException e) { |
| 89 | + e.printStackTrace(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // the default Reload data |
| 94 | + protected void reloadfile(GridWebBean gridweb,HttpServletRequest request, String file) { |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + gridweb.setWidth(Unit.Pixel(800)); |
| 99 | + gridweb.setHeight(Unit.Pixel(400)); |
| 100 | + String filename = null; |
| 101 | + path = request.getServletContext().getRealPath("/"); |
| 102 | + try { |
| 103 | + gridweb.importExcelFile(path + "file\\" + file); |
| 104 | + } catch (Exception e) { |
| 105 | + e.printStackTrace(); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public abstract void reload(GridWebBean gridweb,HttpServletRequest request, HttpServletResponse response); |
| 111 | + |
| 112 | +} |
0 commit comments