Spring Web MVC - Spring Web Flow Working With JasperReports
To handle the actions we use Spring Web Flow. For that the ReportSimplePdfJasperReportAction is created, see the code below
public class ReportSimplePdfJasperReportAction extends AbstractAction{
private EngineSomeEntitiesJasperReport engineSomeEntitiesJasperReport;
public void setEngineSomeEntitiesJasperReport(
EngineSomeEntitiesJasperReport engineSomeEntitiesJasperReport) {
this.engineSomeEntitiesJasperReport = engineSomeEntitiesJasperReport;
}
protected Event doExecute(RequestContext context)throws Exception{
try{
JRBeanCollectionDataSource jRBeanCollectionDataSource =
this.engineSomeEntitiesJasperReport.engine();
context.getRequestScope().put("datasource", jRBeanCollectionDataSource);
}
catch(Exception e){
}
return success();
}
}
Again we are using a setter method to inject our engine class; the doExecute method calls the engine method and recieve the JRBeanCollectionDataSource object. For this example, again no parameters are used (i.e context.getFlowScope().get(...)). An important part of the code is context.getRequestScope().put("datasource", jRBeanCollectionDataSource);
Our ReportSimplePdfJasperReportAction must be declared like a bean too; but we are working with SWF; then for this bean, it is declared in springwebflowjasperreports-beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="reportSimplePdfJasperReportAction"
class="com.springjasperreports.swf.actions.jasperreports.ReportSimplePdfJasperReportAction" >
<property name="engineSomeEntitiesJasperReport" >
<ref bean="idEngineSomeEntitiesJasperReport" />
</property>
</bean>
</beans>
Now we must declare the states of our flow. Then for this flow, we will create springwebflowjasperreports-flow.xml, and see the code below
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">
<start-state idref="springwebflowjasperreports-one" />
<view-state id="springwebflowjasperreports-one"
view="springwebflowjasperreports-stepone">
<transition on="next" to="springwebflowjasperreports-two" />
</view-state>
<view-state id="springwebflowjasperreports-two"
view="springwebflowjasperreports-steptwo">
<transition on="next" to="springwebflowjasperreports-printpdf" />
<transition on="end" to="springwebflowjasperreports-end" />
</view-state>
<view-state id="springwebflowjasperreports-printpdf"
view="reportsimplepdfjasperreport" >
<render-actions>
<action bean="reportSimplePdfJasperReportAction" />
</render-actions>
<!-- I am still using the last jsp of the flow -->
<transition on="next" to="springwebflowjasperreports-printpdf" />
<transition on="end" to="springwebflowjasperreports-end" />
</view-state>
<end-state id="springwebflowjasperreports-end" view="welcome" >
</end-state>
<import resource="springwebflowjasperreports-beans.xml"/>
</flow>
The states declaration is clear by itself. For a better idea, using Spring IDE we can see the flow in a graphical way.
The last part with SWF is to create the configuration. For that, we will create applicationContext-swf.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd">
<bean id="idSpringWebFlowJasperReportsFlowController"
class="org.springframework.webflow.executor.mvc.FlowController">
<property name="flowExecutor" ref="flowExecutorSpringWebFlowJasperReports" />
</bean>
<flow:executor id="flowExecutorSpringWebFlowJasperReports"
registry-ref="flowRegistrySpringWebFlowJasperReports"
repository-type="singlekey"/>
<flow:registry id="flowRegistrySpringWebFlowJasperReports">
<flow:location path="/WEB-INF/flows/springwebflowjasperreports-flow.xml" />
</flow:registry>
</beans>
- Login or register to post comments
- 29749 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
clermont38 replied on Fri, 2009/05/15 - 12:18pm
cnbaluramesh replied on Sat, 2009/06/13 - 4:03am
vijayasaradhi_p replied on Tue, 2009/08/18 - 10:56pm
rajesh_dzone replied on Sun, 2009/09/27 - 9:47pm
zizul replied on Fri, 2009/11/13 - 5:24am
Manuel Jordan replied on Sat, 2009/11/14 - 10:23pm
Hello Guys
The source is almost available.
My hard disk crushed months ago, and the source code is somewhere among many DVD,
Regards