开发者

looking up beans from resources.xml into controller/service in groovy

开发者 https://www.devze.com 2023-04-13 09:00 出处:网络
I am using groovy-grails with jasper reports to develop an app. I need to lookup \'report bean\' based on its parameter (like reportname /id retrived from database, which I will get from customer clic

I am using groovy-grails with jasper reports to develop an app. I need to lookup 'report bean' based on its parameter (like reportname /id retrived from database, which I will get from customer click) - which will be stored as a property of report bean from resources.xml into either the reportcontroller or reportservice. Also I have to 'get' the jrxml template related to this id and the parameter Map, both defined as properties in the bean. I need to know like how can we achieve this and do we need to define any 'managing beans' in the xml which manage these report beans. So for example the report bean will look like as follows:

     <bean id="DeptReport" class="com.myapp.reporting.domain.Report">    
        <property name="reportName" value="Dept Report"/>
        <property name="reportFiles">
          <map>
            <entry key="JasperPrint" value="DeptRoles.jrxml"/>
          </map>
        </property>
        <property name="promptforparameter" value="true"/>
        <property name="parameter" value="department_id"/>
        <property name="displayName" value="report.deptReport.name"/>
     </bean>

There will be many beans like this.

Also the .jrxml file for all reports are stored in a directory and I want to wire that location into the reporting context so that whenever a report is clicked on the front end I want to look up these values from the context into the repo开发者_运维百科rt service/controller to generate the report. I know we have to do like a ctx.getbean(reportId) somewhere but I also want to know how to setup a manager bean with some other properties like template location, datasource, parameter map, reportid and a jasperreportrenderrer object. So this ReportManager bean is loaded reused every time there is a call for another report and persisted across a user session ideally.


You should be able to do something like this:

import org.springframework.context.*

class MyService implements ApplicationContextAware {
  ApplicationContext applicationContext

  def getBeanByName( String name ) {
    applicationContext.getBean( name )
  }
}

That's in a grails Service class, but the same should hold true for a controller

0

精彩评论

暂无评论...
验证码 换一张
取 消