Use Dojo with Vaadin / Inject external file into head block of Vaadin page response


/ Published in: Groovy
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package com.vaadin.terminal.gwt.server;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. /**
  6.  * Created by IntelliJ IDEA.
  7.  * User: laurence
  8.  * Date: Jul 29, 2011
  9.  * Time: 8:06:44 PM
  10.  *
  11.  * com.vaadin.terminal.gwt.server.ApplicationServlet2
  12.  *
  13.  * Needed to create this for Dojo to work with Vaadin.
  14.  *
  15.  * This looks for a file named header.inc.htm in
  16.  * dir getServletContext().getRealPath(".");
  17.  * and injects it into a vaadin page response (providing it exists)
  18.  *
  19.  * You must change your vaadin servlet name in web.xml
  20.  * from: com.vaadin.terminal.gwt.server.ApplicationServlet
  21.  * to: com.vaadin.terminal.gwt.server.ApplicationServlet2
  22.  *
  23.  * Also noticed the inline dojo method script button onClick events
  24.  * do not work in Vaadin custom layout templates
  25.  * but the new dojo html5 way of handling button clicks does work
  26.  *
  27.  * Also had to use dojo to update the body class to the tundra style
  28.  *
  29.   <script type="text/javascript">
  30.   // <![CDATA[
  31.   dojo.ready( function() { dojo.addClass( dojo.body() , "tundra"); } );
  32.   // ]]>
  33.   </script>
  34.  *
  35.  *
  36.  */
  37.  
  38.  
  39. public class ApplicationServlet2
  40. extends com.vaadin.terminal.gwt.server.ApplicationServlet {
  41.  
  42. public ApplicationServlet2() {
  43. super();
  44. }
  45.  
  46. def String doTemplate(String aStrTemplate, Map aBindingMap) {
  47. def engine = new groovy.text.SimpleTemplateEngine();
  48. def template = engine.createTemplate(aStrTemplate).make(aBindingMap);
  49. return template.toString();
  50. }
  51.  
  52. def writeDebugString1( BufferedWriter a_page, String a_path ) {
  53. // ij ide """ syntax highlighting workaround
  54. def sDebug1_templ = '''
  55. <!--
  56. header.inc.htm
  57. web path: .
  58. file path: ${a_path}
  59. -->
  60. '''.trim();
  61. def sDebug1 = doTemplate(sDebug1_templ, ["a_path": a_path] as Map);
  62.  
  63. a_page.write(sDebug1 as String);
  64. }
  65.  
  66. @Override
  67. protected void writeAjaxPageHtmlHeadStart(BufferedWriter page,
  68. HttpServletRequest request)
  69. super.writeAjaxPageHtmlHeadStart(page, request);
  70.  
  71. def sPath1 = this.getServletContext().getRealPath(".");
  72. def sPath2 = new File(sPath1).getCanonicalPath();
  73. def sPath3 = "${sPath2}${File.separator}header.inc.htm";
  74.  
  75. def fo = new File(sPath3);
  76.  
  77. page.write("\n<!-- header.inc.htm beg -->\n");
  78. writeDebugString1( page, sPath3 );
  79.  
  80. if (fo.exists()) {
  81. def sHeaderInc = fo.text;
  82. page.write(sHeaderInc as String);
  83. }
  84.  
  85. page.write("\n<!-- header.inc.htm end -->\n\n");
  86. }
  87.  
  88. //
  89. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.