To use RTFTemplate into WEB Application, you can implement abstract servlet RTFTemplateServlet. This servlet use RTF Velocity Reader strategy.
To use it, you must :
Methods which must be implement are :
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); } }
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>
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.