Problem:
This article illustrates how can we use the combination of Struts2.0 and Spring3.0. The combination might be required when you need the strength of Spring like dependency injection and jdbc support to be used with in the Struts 2.0 application.
Now lets start with the core files required:
The core file is the deployment descriptor which should be something like this:
(1) web.xml
(3) jdbc.properties- for database connectivity
(4) cronjob.properties- for scheduling events
(5) package.properties: for message bundle
(6) struts.xml - core file of struts2 framework.It defines the details of the actions.
(7) struts.properties (8) index.jsp- a file that is loaded in first and redirects to desirrd action
(9) The pmdashboard.action entry in the index.jsp is checked in the struts.xml (and any such xmls specified in struts.xml file itself).Here, it resembles something like this from struts.xml: (10) So the redirection is made into Dashboard.jsp page. This page can be any simple jsp page, from where you can provide link to particular actions. The link can be something like this:
It is to be noted that <s></s> is the struts core tag.
Now see the generateReports action in struts.xml(and actions.xml) file.
(11) We see in actions.xml the entry:
(12) Digging out gradually we get the control goes to the reportService bean, which is ReportService.java(as specified in applicationContext.xml).
The method generateallReports(..) of ReportServices.java is invoked, which simply has the following structure:
(13) The retrun typ string indicates that we are returning control from here and determine the result page accordingly.(If success is returned then the redirection is made to the page /pages/Graphs.jsp).
(14) The ReportController.java is simply a java class that extends ActionSupport (from Struts 2.0). By extending this class, we get access to the method with signature as shown below:
(15) This is the default method to be called, but the method to be invoked can be changed on our own need basis. I have invoked the method generateAllReports (but via ReportService.java). The generateAllReports method in ReportController.java has following format:
(16) The jars required for Struts can be found in Struts home page
and the jar named struts2-spring-plugin-2.2.3.1.jar is required for the combination to work.
So the above example shows how to use Struts2.0 and Spring3.0 united their strengths to make a robust system.
(4) cronjob.properties- for scheduling events
(5) package.properties: for message bundle
(6) struts.xml - core file of struts2 framework.It defines the details of the actions.
(7) struts.properties (8) index.jsp- a file that is loaded in first and redirects to desirrd action
(9) The pmdashboard.action entry in the index.jsp is checked in the struts.xml (and any such xmls specified in struts.xml file itself).Here, it resembles something like this from struts.xml: (10) So the redirection is made into Dashboard.jsp page. This page can be any simple jsp page, from where you can provide link to particular actions. The link can be something like this:
It is to be noted that <s></s> is the struts core tag.
Now see the generateReports action in struts.xml(and actions.xml) file.
(11) We see in actions.xml the entry:
(12) Digging out gradually we get the control goes to the reportService bean, which is ReportService.java(as specified in applicationContext.xml).
The method generateallReports(..) of ReportServices.java is invoked, which simply has the following structure:
(13) The retrun typ string indicates that we are returning control from here and determine the result page accordingly.(If success is returned then the redirection is made to the page /pages/Graphs.jsp).
(14) The ReportController.java is simply a java class that extends ActionSupport (from Struts 2.0). By extending this class, we get access to the method with signature as shown below:
(15) This is the default method to be called, but the method to be invoked can be changed on our own need basis. I have invoked the method generateAllReports (but via ReportService.java). The generateAllReports method in ReportController.java has following format:
(16) The jars required for Struts can be found in Struts home page
and the jar named struts2-spring-plugin-2.2.3.1.jar is required for the combination to work.
So the above example shows how to use Struts2.0 and Spring3.0 united their strengths to make a robust system.
Happy Coding!
No comments:
Post a Comment