1.struts2与jfreechart结合
(1),查看帮助文档即可以知道如何配置。首先把struts2文件夹下的lib中的 struts2-jfreechart-plugin-2.1.8.1.jar包加入到项目下,注意此时的struts2-jfreechart- plugin-2.1.8.1.jar中的struts-plugin.xml文件中应该为:
400
600
(3)在action中写上类似的内容,如:
package com.biped.struts.action;
import java.awt.Font;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class SelectAction extends ActionSupport{
private JFreeChart chart;//必须提供该属性,提供getter方法,且名字要为chart
private List
public JFreeChart getChart() {//需要自己产生JFreeChart对象
JFreeChart chart = ChartFactory.createBarChart3D("爱好兴趣调查", "项目","比例", createDataset(), PlotOrientation.VERTICAL, true, true,true);
TextTitle tt = chart.getTitle();
tt.setFont(new Font("黑体",Font.BOLD,20));
chart.getLegend().setItemFont(new Font("宋体",Font.ITALIC,10));
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(new Font("宋体",Font.ITALIC,14));
domainAxis.setTickLabelFont(new Font("宋体",Font.ITALIC,14));
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
ValueAxis valueAxis = plot.getRangeAxis();
valueAxis.setLabelFont(new Font("宋体",Font.ITALIC,14));
valueAxis.setTickLabelFont(new Font("宋体",Font.ITALIC,14));
return chart;
}
public List
return interest;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
public void setInterest(List
this.interest = interest;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
@SuppressWarnings("unchecked")
public Map addSelectResult(List
//这里只是为了模拟,放在Application中
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();
for(String str : list){
if(null==map.get(str)){
map.put(str,1);
}else{
map.put(str, (Integer)map.get(str)+1);
}
}
return map;
}
@SuppressWarnings("unchecked")
public CategoryDataset createDataset(){//收集数据,并产生相应的Dataset
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Map map = addSelectResult(this.getInterest());
dataset.setValue((Integer)map.get("football"), "a", "足球");
dataset.setValue((Integer)map.get("basketball"), "b", "篮球");
dataset.setValue((Integer)map.get("volleyball"), "c", "排球");
dataset.setValue((Integer)map.get("pingpong"), "d", "乒乓球");
dataset.setValue((Integer)map.get("badminton"), "e", "羽毛球");
return dataset;
}
}
(4).jsp中的内容:
<s:form action="select">
<s:checkbox label="足球" name="interest" fieldValue="football" labelposition="left">
<s:checkbox label="篮球" name="interest" fieldValue="basketball" labelposition="left">
<s:checkbox label="排球" name="interest" fieldValue="volleyball" labelposition="left">
<s:checkbox label="乒乓球" name="interest" fieldValue="pingpong" labelposition="left">
<s:checkbox label="羽毛球" name="interest" fieldValue="badminton" labelposition="left">
<s:submit>
(5).结果:
2.和JFreeChart类似,从lib目录下拷贝struts2-jasperreports-plugin-2.1.8.1.jar包到项目中,其中的struts-plugin.xml应该为这样:
接下来配置struts.xml,可以参见struts2的帮助文档,从文档可以知道,现在的jasperreport插件基本上都是只支持list 类型的数据源,因此要是数据源是如JRDataSource等的,可以先存在list中,然后再进行配置,文档中说明了几个参数是必须要指定的,如:
<result name="success"
type="jasper"
>
<param name="location"
>
foo.jasper
<param name="dataSource"
>
mySource
<param name="format"
>
CSV
仿照其配置,可以写上:
最后在对应的ReportAction上的java内容为:
package com.biped.struts2.action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.biped.struts2.domain.User;
import com.opensymphony.xwork2.ActionSupport;
public class ReportAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
private List
public List
return getDataSource();
}
public void setList(List
this.list = list;
}
// 这里把连接数据库的方法放在一起
private static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost:3306/drp";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(dbUrl, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
@SuppressWarnings("static-access")//取出数据并填充list
public List
String sql = "select * from t_user";
Connection conn = this.getConnection();
PreparedStatement statement = null;
ResultSet result = null;
List
try {
User user = null;
statement = conn.prepareStatement(sql);
result = statement.executeQuery();
while (result.next()) {
user = new User();
user.setUserId(result.getString("user_id"));
user.setUserName(result.getString("user_name"));
user.setPassword(result.getString("password"));
user.setEmail(result.getString("email"));
user.setCreateDate(result.getTimestamp("create_date"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (result != null) {
result.close();
result = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
User.java:
package com.biped.struts2.domain;
import java.util.Date;
public class User {
private String userId;
private String userName;
private String password;
private String email;
private String phone;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return userId+","+userName+","+password+","+phone+","+email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
private Date createDate;
}
结果:
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yakoo5/archive/2010/05/31/5638226.aspx