Developer guide

How integrate RTFTemplate into WEB application

To use RTFTemplate into WEB Application, you can implement abstract servlet RTFTemplateServlet. This servlet use RTF Velocity Reader strategy.

To use it, you must :

Extends RTFTemplateServlet

Methods which must be implement are :

  • getRTFReader : must return Reader of the RTF model source to use.
  • putContext : in this method, put the context of your model. For the collection it's very important to put at least one JAVA object, in order to RTFTemplate generate correctly the velocity macro #foreach (for manage iteration).
  • getXMLFieldsAvailable : must return XML fileds available for the RTF model or null if there is not XML Fileds available.
  • getFileNameOfContentDisposition : to return name of filename to use (See Content-Disposition of request) when response is finished.
  • getGroupByPerPageBreak : if you want manage page break, in your RTF model. See How manage page break ? for more informations. []

    Here an example of class which implement RTFTemplateServlet :

      ...
      import net.sourceforge.rtf.web.servlet.RTFTemplateServlet;
    
      public class SampleRTFTemplateServlet extends RTFTemplateServlet  {
    
            public static final long serialVersionUID = 1L;
            
            private static final String JAKARATA_VELOCITY_MODEL = "jakarta-velocity-model";
            
            protected Reader getRTFReader(HttpServletRequest request, HttpServletResponse response) throws Exception {
                    // 1. Get Real path of RTF model.
                    String rtfModelName = request.getParameter("modelName");
                    rtfModelName+= ".rtf";
                    String rtfModelPath = "/models/jakartavelocityproject/" + rtfModelName; 
                    rtfModelPath = super.getRealPathOfRTFModel(request, rtfModelPath);
                    
                    // 2. Get Reader of RTF model
                    Reader rtfModelReader = new FileReader(new File(rtfModelPath));
                    return rtfModelReader;
            }
            
            protected void putContext(HttpServletRequest request, HttpServletResponse response, Context ctx ) throws Exception {
                    // Swith RTF Model Name, Context is different
                    String rtfModelName = request.getParameter("modelName");
                    if (JAKARATA_VELOCITY_MODEL.equals(rtfModelName)) {
                            putContextJAKARATA_VELOCITY_MODEL(request, response, ctx);
                    }
                    else {
                            // ...... Other RTF model
                    }
            }
            
            protected InputStream getXMLFieldsAvailable(HttpServletRequest request, HttpServletResponse response) throws Exception {
                    //      Swith RTF Model Name, XML fields available is different or can be null
                    String rtfModelName = request.getParameter("modelName");
                    if (JAKARATA_VELOCITY_MODEL.equals(rtfModelName)) {
                            String xmlFieldsAvailable = rtfModelName + ".fields.xml";
                            InputStream inputStream = SampleRTFTemplateServlet.class.getResourceAsStream(xmlFieldsAvailable);
                            return inputStream;
                    }
                    else {
                            // ...... Other RTF model
                    }
                    return null;
            }
            
            protected void putContextJAKARATA_VELOCITY_MODEL(HttpServletRequest request, HttpServletResponse response, Context ctx ) throws Exception {
                    
                    String projectName = request.getParameter("projectName");
                    /*
                     * Context of simply POJO
                     */
                    ctx.put("date", new Date());
                    ctx.put("project", new Project(projectName));
            }
            
            /**
             * return name of FileName for Content-Disposition
             */
            protected String getFileNameOfContentDisposition(HttpServletRequest request) {
                    String rtfModelName = request.getParameter("modelName");
                    if (rtfModelName != null) {
                            return rtfModelName + ".rtf";
                    }
                    return super.getFileNameOfContentDisposition(request);
            }
    }
    

RTF servlet declaration

Once you have implement RTFTemplate, declare it into web.xml like this :

  <!-- Servlet RTFtemplate -->   
  <servlet>
    <servlet-name>rtf</servlet-name>
    <servlet-class>net.sourceforge.rtf.web.usecases.SampleRTFTemplateServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>rtf</servlet-name>
    <url-pattern>/rtfservlet/*</url-pattern>
  </servlet-mapping>  

RTF servlet calling

To launch RTF edition, with servlet, you can declare form, with action which is linked into RTF servlet.

  <form name="sampleRTFTemplate" action="/youWebApplication/rtfservlet" >
  
    <input type="submit" value="Launch RTF Edition" >
    
  </form>
  

where youWebApplication is name of your Web Application.