FreeMarker与Struts2结合使用
一、模板文件 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>
<p id="container"></p>
</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> <p id="container"></p> </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>
<p id="container"></p>
</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>
如果有想要源码的可以给我留言哈!
分享到:
推荐阅读
-
浅要分析Python程序与C程序的结合使用
-
socket io与vue-cli的结合使用的示例代码
-
Bootstrap进度条与AJAX后端数据传递结合使用实例详解
-
JSPSupportServlet Struts2与FreeMarker配合使用
-
Vue中JS动画与Velocity.js的结合使用
-
vue指令之表单控件绑定v-model v-model与v-bind结合使用
-
Docker的使用初探(二):Docker与.NET Core的结合
-
基于jquery的文本框与autocomplete结合使用(asp.net+json)
-
vue2.0 与 bootstrap datetimepicker的结合使用实例
-
解析GridView自带分页及与DropDownList结合使用