{"id":844,"date":"2006-08-25T20:27:10","date_gmt":"2006-08-26T04:27:10","guid":{"rendered":"http:\/\/cephas.net\/blog\/2006\/08\/25\/hacking-webwork-result-types-freemarker-to-html-to-javascript\/"},"modified":"2006-08-25T20:27:10","modified_gmt":"2006-08-26T04:27:10","slug":"hacking-webwork-result-types-freemarker-to-html-to-javascript","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2006\/08\/25\/hacking-webwork-result-types-freemarker-to-html-to-javascript\/","title":{"rendered":"Hacking WebWork Result Types: Freemarker to HTML to JavaScript"},"content":{"rendered":"<p>I threw all of my past experience with Struts out the window when I started my new job because we use WebWork. WebWork is approximately one thousand times better though so I&#8217;m not complaining.  One of the unique to WebWork features (as compared to Struts) is the notion of a <a href=\"http:\/\/www.opensymphony.com\/webwork\/wikidocs\/Result%20Types.html\">result type<\/a> which is the process responsible for combining the model with a some kind of template (usually JSP or FreeMarker) to create a view. <\/p>\n<p>Anyway, for various reasons we&#8217;ve found it useful at work to output some pieces of content usually rendered as HTML instead as JavaScript via <code>document.write()<\/code>.  Because it would be a nightmare to maintain two FreeMarker templates, one for HTML and one for JavaScript output, I figured out a way to transform a FreeMarker template that normally renders HTML to render JavaScript using a custom WebWork result type.  Simply extend the <a href=\"http:\/\/www.opensymphony.com\/webwork\/api\/com\/opensymphony\/webwork\/views\/freemarker\/FreemarkerResult.html\">FreemarkerResult<\/a. class and override the <a href=\"http:\/\/www.opensymphony.com\/webwork\/api\/com\/opensymphony\/webwork\/views\/freemarker\/FreemarkerResult.html#doExecute(java.lang.String,%20com.opensymphony.xwork.ActionInvocation)\"><code>doExecute()<\/code><\/a> method with code that will look extremely similar to the existing doExecute() method. I&#8217;ll highlight the differences by showing you two lines from the existing doExecute() method:<\/p>\n<pre>\r\n\/\/ Process the template\r\ntemplate.process(model, getWriter());\r\n<\/pre>\n<p>and then the updated version that transforms the HTML into JavaScript using the <a href=\"http:\/\/jakarta.apache.org\/commons\/lang\/api\/org\/apache\/commons\/lang\/StringEscapeUtils.html\">StringEscapeUtils<\/a> class from the <a href=\"http:\/\/jakarta.apache.org\/commons\/lang\/\">commons lang<\/a> library:<\/p>\n<pre>\r\n\/\/ Process the template\r\nStringWriter writer = new StringWriter();\r\ntemplate.process(model, writer);\r\ngetWriter().write(\"document.write('\" + StringEscapeUtils.escapeJavaScript(writer.toString()) + \"')\");\r\n<\/pre>\n<p>The <a href=\"http:\/\/www.opensymphony.com\/webwork\/wikidocs\/XWork%20Configuration.html\">xwork config file<\/a> changes a bit as well. Here&#8217;s both the HTML version and the JavaScript version:<\/p>\n<pre>\r\n&lt;result name=\"html\" type=\"freemarker\"&gt;foo.ftl&lt;\/result&gt;\r\n&lt;result name=\"javascript\" type=\"freemarkerhtml2javascript\"&gt;\r\n  &lt;param name=\"location\"&gt;foo.ftl&lt;\/param&gt;\r\n  &lt;param name=\"contentType\"&gt;text\/javascript&lt;\/param&gt;\r\n&lt;\/result&gt;\r\n<\/pre>\n<p>The end result is that with a little logic added to your WebWork action, you can include the result of that action in your own site:<\/p>\n<pre>\r\n&lt;ww:action name=\"viewSnippet\" executeResult=\"true\" \/&gt;\r\n<\/pre>\n<p> and enable other people to use the same result via embedded JavaScript:<\/p>\n<pre>\r\n&lt;script src=\"http:\/\/yoursite.com\/yourSnippet.jspa?format=javascript\"&gt;&lt;\/script&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I threw all of my past experience with Struts out the window when I started my new job because we use WebWork. WebWork is approximately one thousand times better though so I&#8217;m not complaining. One of the unique to WebWork features (as compared to Struts) is the notion of a result type which is the &hellip; <a href=\"https:\/\/cephas.net\/blog\/2006\/08\/25\/hacking-webwork-result-types-freemarker-to-html-to-javascript\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hacking WebWork Result Types: Freemarker to HTML to JavaScript<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,29,22,30],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/844"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/844\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}