package com.pes_soft.example.ctrler;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.pes_soft.example.constant.IReportFormat;
import com.pes_soft.example.constant.IReportParam;
import com.pes_soft.example.ds.IReportDataSourceProvider;
import com.pes_soft.example.model.JavaBeanColor;
import com.pes_soft.example.model.JavaBeanFruit;
import com.pes_soft.example.model.JavaBeanPerson;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
/**
* 返回iReport报表视图
* @param model
* @return
*/
@RequestMapping(value = "/report", method = RequestMethod.GET)
public String report(Model model) {
// 报表数据源
JRDataSource jrDataSourceMain_Person = null;
JRDataSource jrDataSourceSub_Color = null;
JRDataSource jrDataSourceSub_Fruit = null;
try {
IReportDataSourceProvider dataSourcePerson = new IReportDataSourceProvider(JavaBeanPerson.class, JavaBeanPerson.getList());
jrDataSourceMain_Person = dataSourcePerson.create(null);
IReportDataSourceProvider dataSourceColor = new IReportDataSourceProvider(JavaBeanColor.class, JavaBeanColor.getList());
jrDataSourceSub_Color = dataSourceColor.create(null);
IReportDataSourceProvider dataSourceFruit = new IReportDataSourceProvider(JavaBeanFruit.class, JavaBeanFruit.getList());
jrDataSourceSub_Fruit = dataSourceFruit.create(null);
} catch (JRException e) {
e.printStackTrace();
}
model.addAttribute("url", "/WEB-INF/jasper/MvcMultiDsCrossReportExample.jasper"); // 报表模板
model.addAttribute("format", IReportFormat.pdf.name()); // 报表输出格式
model.addAttribute("jrMainDataSource", jrDataSourceMain_Person); // 报表主数据源
model.addAttribute("jrColorDataSource", jrDataSourceSub_Color); // 报表子数据源1(柱状图[Bar Chart])
model.addAttribute("jrFruitDataSource", jrDataSourceSub_Fruit); // 报表子数据源2(饼图[Pie Chart])
return IReportParam.IREPORT_VIEW; // 对应jasper-views.xml中的bean id
}
}
- 1
- 2
前往页