sloppycode.net
DataProducer
A class for separating the presentation layer from the data layer in Java.
Home
›
Code snippets
›
Java
›
DataProducer
Download Dataproducer
Dataproducer, available for php,java servlets and asp is used to seperate the presentation layer from the data layer in web applications. With it you can specify single and multiple row tags inside a html template, which are then replaced with data that you specify. Please note this is the first version of the class and features little error handling. More features and error handling will be added in the next version. For this Java version, tag libaries would be a better solution most of the time.
Methods
dp.doDataProducer(startTag,endTag,data,contents)
Replaces all data between starttag and endtag, with the contents of data, a Vector with each element being a Hashtable. The template it uses is the 'contents' variable. Returns the parsed template as a string.
dp.doSingleDataProducer(data,contents)
Replaces single data tags, specified in data which is a Hashtable. 'contents' is the html template, the method returns the parsed template as a string.
dp.openTemplate(filename)
Opens 'filename' as a template (usually a html file), and returns it as a string.
Example
The example opens a html template, replaces 3 single tags, and 3 rows of data
/* DataProducer Example */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class Example extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { String templatefile; response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Open template html file + add to string DataProducer dp = new DataProducer(); // Alter this to reflect the path to your template file if necessary templatefile = getServletContext().getRealPath("/")+"template.html"; String contents = dp.openTemplate(templatefile); // Single data Hashtable singledata = new Hashtable(); singledata.put("firstname","John"); singledata.put("surname","Smith"); singledata.put("fruit","Apple"); contents = dp.doSingleDataProducer(singledata,contents); // Multiple data Hashtable row1 = new Hashtable(); Hashtable row2 = new Hashtable(); Hashtable row3 = new Hashtable(); row1.put("product_name","Can of beans"); row1.put("product_code","#23A534EE25"); row1.put("product_price","12.34"); row2.put("product_name","LCD monitor"); row2.put("product_code","#87J56NFG56"); row2.put("product_price","222.99"); row3.put("product_name","Lampshade"); row3.put("product_code","#2RE4YH4H5"); row3.put("product_price","36.00"); Vector multirows = new Vector(); multirows.add(row1); multirows.add(row2); multirows.add(row3); contents = dp.doDataProducer("","",multirows,contents); out.println(contents); } }
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon