MVC是多年以來一直較為優秀的框架。對於java來說,Mode經常是由JavaBean擔當的,Controller是由Servlet擔當,View是由JSP擔當的。JSP確實很優秀,功能強大,甚至可以直接在裡面寫java代碼(scriptlet),他的自定義標簽功能更是強大無比。然而在JSP可以被直接訪問,所以不是完全符合MVC的標准,直接寫java代碼導致後期維護和可擴展性大大的降低了,其實個人覺得這個完全可以從人為上去改變,大不了不在JSP中寫java代碼。每次都讓客戶端訪問Servlet後再跳轉到JSP,這都是完全可以由編程人員解決的事情。在JSP推出後,有兩款比較出名的模板引擎可以完全代替JSP,那就是Velocity和FreeMarker。由於FreeMarker是在Velocity之後出來的所以其綜合功能要比Velocity強大。這兩天自己試著寫了個Freemarker和Struts2結合的Demo,其中模板是隨便寫了個jQuery的圖標工具--highcharts。 一、模板文件 highcharts1.ftl [java] <PRE class=html name="code"><html></PRE><PRE class=html name="code"> <head> <title>jqGrid&FreeMarker Test</title> <script src="js/jquery-1.8.3.js" type="text/javascript"></script> <script src="js/highcharts.js" type="text/javascript"></script> <script type="text/javascript" > $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'column', //spline曲線line直線pie餅狀bar橫向條狀scatter 散狀等 marginRight: 100, marginBottom: 25 }, title: { text: '2012年氣溫變化表', x: 0 //center }, subtitle: { text: '合肥氣象台提供', x: 0 }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, yAxis: { title: { text: '溫度 (°C)' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +'°C'; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: 0, y: 0, borderWidth: 0 }, series: [{name: '馬鞍山',data: [<#list mas as temp>${temp},</#list>]}, { name: '蕪湖',data: [<#list wh as temp>${temp},</#list>]},{name: '合肥',data: [<#list hf as temp>${temp},</#list>]}] //這裡是用了FreeMarker的遍歷,其他地方都是highcharts的固定用法,不用管它 }); }); }); </script> <style type="text/css"> #container{ width:70%; height 300px; } </style> </head> <body> <div id="container"></div> </body> </html></PRE><BR> 二、模板解析java類中的方法<PRE class=java name="code">public void highchartsResolution(String templateFileName, String htmlName) { List<Integer> list = null; Writer out = null; Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading( ServletActionContext.getServletContext(), "TemplateFiles"); cfg.setDefaultEncoding("UTF-8"); Map root = new HashMap(); try { Template template = cfg.getTemplate(templateFileName); String path = ServletActionContext.getServletContext().getRealPath( "/"); File file = new File(path + htmlName); out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); root.put("hf", tdao.getTemperatureByCity("合肥")); root.put("mas", tdao.getTemperatureByCity("馬鞍山")); root.put("wh", tdao.getTemperatureByCity("蕪湖")); template.process(root, out); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }</PRE><BR> <BR> <BR> 三、action中的執行方法<BR> <SPAN style="WHITE-SPACE: pre"></SPAN><PRE class=java name="code">public String execute() throws Exception { TemplateResolution tr = new TemplateResolution(); tr.highchartsResolution("highcharts1.ftl","test.html"); return Action.SUCCESS; }</PRE><BR> <BR> <BR> 三、struts.xml配置<BR> <PRE class=html name="code"><action name="test" class="com.lubby.action.ServiceAction"> <result type="redirect">/test.html</result> </action></PRE> <PRE></PRE> <P><BR> </P> <P>如果有想要源碼的可以給我留言哈!</P> [html] <html> <html>[html] view plaincopyprint? <head> <title>jqGrid&FreeMarker Test</title> <script src="js/jquery-1.8.3.js" type="text/javascript"></script> <script src="js/highcharts.js" type="text/javascript"></script> <script type="text/javascript" > $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'column', //spline曲線line直線pie餅狀bar橫向條狀scatter 散狀等 marginRight: 100, marginBottom: 25 }, title: { text: '2012年氣溫變化表', x: 0 //center }, subtitle: { text: '合肥氣象台提供', x: 0 }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, yAxis: { title: { text: '溫度 (°C)' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +'°C'; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: 0, y: 0, borderWidth: 0 }, series: [{name: '馬鞍山',data: [<#list mas as temp>${temp},</#list>]}, { name: '蕪湖',data: [<#list wh as temp>${temp},</#list>]},{name: '合肥',data: [<#list hf as temp>${temp},</#list>]}] //這裡是用了FreeMarker的遍歷,其他地方都是highcharts的固定用法,不用管它 }); }); }); </script> <style type="text/css"> #container{ width:70%; height 300px; } </style> </head> <body> <div id="container"></div> </body> </html> <head> <title>jqGrid&FreeMarker Test</title> <script src="js/jquery-1.8.3.js" type="text/javascript"></script> <script src="js/highcharts.js" type="text/javascript"></script> <script type="text/javascript" > $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'column', //spline曲線line直線pie餅狀bar橫向條狀scatter 散狀等 marginRight: 100, marginBottom: 25 }, title: { text: '2012年氣溫變化表', x: 0 //center }, subtitle: { text: '合肥氣象台提供', x: 0 }, xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] }, yAxis: { title: { text: '溫度 (°C)' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +'°C'; } }, legend: { layout: 'vertical', align: 'right', verticalAlign: 'top', x: 0, y: 0, borderWidth: 0 }, series: [{name: '馬鞍山',data: [<#list mas as temp>${temp},</#list>]}, { name: '蕪湖',data: [<#list wh as temp>${temp},</#list>]},{name: '合肥',data: [<#list hf as temp>${temp},</#list>]}] //這裡是用了FreeMarker的遍歷,其他地方都是highcharts的固定用法,不用管它 }); }); }); </script> <style type="text/css"> #container{ width:70%; height 300px; } </style> </head> <body> <div id="container"></div> </body> </html> 二、模板解析java類中的方法[java] view plaincopyprint?public void highchartsResolution(String templateFileName, String htmlName) { List<Integer> list = null; Writer out = null; Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading( ServletActionContext.getServletContext(), "TemplateFiles"); cfg.setDefaultEncoding("UTF-8"); Map root = new HashMap(); try { Template template = cfg.getTemplate(templateFileName); String path = ServletActionContext.getServletContext().getRealPath( "/"); File file = new File(path + htmlName); out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); root.put("hf", tdao.getTemperatureByCity("合肥")); root.put("mas", tdao.getTemperatureByCity("馬鞍山")); root.put("wh", tdao.getTemperatureByCity("蕪湖")); template.process(root, out); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } public void highchartsResolution(String templateFileName, String htmlName) { List<Integer> list = null; Writer out = null; Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading( ServletActionContext.getServletContext(), "TemplateFiles"); cfg.setDefaultEncoding("UTF-8"); Map root = new HashMap(); try { Template template = cfg.getTemplate(templateFileName); String path = ServletActionContext.getServletContext().getRealPath( "/"); File file = new File(path + htmlName); out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file))); root.put("hf", tdao.getTemperatureByCity("合肥")); root.put("mas", tdao.getTemperatureByCity("馬鞍山")); root.put("wh", tdao.getTemperatureByCity("蕪湖")); template.process(root, out); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } finally { try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } 三、action中的執行方法 [java] public String execute() throws Exception { TemplateResolution tr = new TemplateResolution(); tr.highchartsResolution("highcharts1.ftl","test.html"); return Action.SUCCESS; } public String execute() throws Exception { TemplateResolution tr = new TemplateResolution(); tr.highchartsResolution("highcharts1.ftl","test.html"); return Action.SUCCESS; } 三、struts.xml配置 [html] <action name="test" class="com.lubby.action.ServiceAction"> <result type="redirect">/test.html</result> </action> <action name="test" class="com.lubby.action.ServiceAction"> <result type="redirect">/test.html</result> </action> 如果有想要源碼的可以給我留言哈! 分享到: