Struts2学习笔记19:Struts2与JFreeChart的整合

 

Struts2学习笔记19

Struts2JFreeChart的整合

第二十三讲

学习内容:

Struts2与JFreeChart的整合实例

下图是对struts2和JFreeChart整合的流程图解

详细步骤:

 1.修改Struts2的lib目录

struts2-jfreechart-plugin-2.0.11.2.jar 中的struts-plugin.xml文件,使package继承于 struts-default

 2.新建项目jfreechart

 3.导入相关的包

 4.index.jsp页面中,在body标签中写入如下代码:

  1. <h1 style="color: red">选择你所喜欢的运动项目</h1>
  2.   <s:form action="interest" >
  3.    <s:checkbox name="sport" fieldValue="football" label="足球"></s:checkbox>
  4.    <s:checkbox name="sport" fieldValue="basketball" label="篮球"></s:checkbox>
  5.    <s:checkbox name="sport" fieldValue="volleyball" label="排球"></s:checkbox>
  6.    <s:checkbox name="sport" fieldValue="pingpong" label="乒乓球"></s:checkbox>
  7.    <s:checkbox name="sport" fieldValue="badminton" label="羽毛球"></s:checkbox>
  8.    <s:submit></s:submit>
  9.   </s:form>

 5.建立struts.xml文件,代码:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="struts2" extends="jfreechart-default">
  7. <action name="interest" class="action.InterestAction">
  8. <result name="success" type="chart">
  9. <param name="height">600</param>
  10.      <param name="width">800</param>
  11. </result>
  12. </action>
  13. </package>
  14. </struts>

 6.建立 action.InterestAction.java文件,代码:

  1. package action;
  2. import java.awt.Font;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.jfree.chart.ChartFactory;
  6. import org.jfree.chart.JFreeChart;
  7. import org.jfree.chart.axis.CategoryAxis;
  8. import org.jfree.chart.axis.CategoryLabelPositions;
  9. import org.jfree.chart.axis.NumberAxis;
  10. import org.jfree.chart.plot.CategoryPlot;
  11. import org.jfree.chart.plot.PlotOrientation;
  12. import org.jfree.chart.title.TextTitle;
  13. import org.jfree.data.category.CategoryDataset;
  14. import org.jfree.data.category.DefaultCategoryDataset;
  15. import com.opensymphony.xwork2.ActionContext;
  16. import com.opensymphony.xwork2.ActionSupport;
  17. public class InterestAction extends ActionSupport {
  18. private static final long serialVersionUID = 180794008736027209L;
  19. private JFreeChart chart;
  20. private List<String> sport;
  21. public List<String> getSport() {
  22. return sport;
  23. }
  24. public void setSport(List<String> sport) {
  25. this.sport = sport;
  26. }
  27. @Override
  28. public String execute() throws Exception {
  29. return SUCCESS;
  30. }
  31. //设置chart
  32. public JFreeChart getChart() {
  33. chart = ChartFactory.createBarChart("运动项目""运动项目""选择人数"this
  34. .createCategory(), PlotOrientation.VERTICAL, falsefalse,
  35. false);
  36. chart.setTitle( new TextTitle("喜欢的运动项目"new Font("隶书" , Font.BOLD , 30)));
  37. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  38. CategoryAxis axis = plot.getDomainAxis();
  39. axis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  40. //设置项目名的倾斜角度
  41. axis.setCategoryLabelPositions( CategoryLabelPositions.UP_45);
  42. axis.setTickLabelFont(new Font("隶书" , Font.BOLD , 15));
  43. //设置项目名的字体类型
  44. NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
  45. numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  46. return chart;
  47. }
  48. @SuppressWarnings("unchecked")
  49. //获取ServletContext的上下文,将选择的数据暂时性的保存于Context中
  50. private void insertResult(List<String> list) {
  51. ActionContext context = ActionContext.getContext();
  52. Map m = context.getApplication();
  53. for (String src : list) {
  54. if (null == m.get(src)) {
  55. m.put(src, 1);
  56. else {
  57. m.put(src, (Integer) m.get(src) + 1);
  58. }
  59. }
  60. }
  61. @SuppressWarnings("unchecked")
  62. //为JFreechart创建数据
  63. private CategoryDataset createCategory() {
  64. DefaultCategoryDataset dcd = new DefaultCategoryDataset();
  65. this.insertResult(this.getSport());
  66. ActionContext context = ActionContext.getContext();
  67. Map m = context.getApplication();
  68. dcd.setValue((Integer) m.get("football"), "足球""足球");
  69. dcd.setValue((Integer) m.get("basketball"), "篮球""篮球");
  70. dcd.setValue((Integer) m.get("volleyball"), "排球""排球");
  71. dcd.setValue((Integer) m.get("pingpong"), "乒乓球""乒乓球");
  72. dcd.setValue((Integer) m.get("badminton"), "羽毛球""羽毛球");
  73. return dcd;
  74. }
  75. }

 7.在web.xml中添加filter

  1. <filter>
  2. <filter-name>struts2</filter-name>
  3. <filter-class>
  4. org.apache.struts2.dispatcher.FilterDispatcher
  5. </filter-class>
  6. </filter>
  7. <filter-mapping>
  8. <filter-name>struts2</filter-name>
  9. <url-pattern>/*</url-pattern>
  10. </filter-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值