简体   繁体   中英

Pass value from gsp to controller

I was using an export plugin for my grails app. How can I pass a list instance to the controller? I have this in my gsp:

    <g:set var="fruitInstanceList" name="fruitInstanceList" value="${fruitInstanceList}"/>
    <export:formats formats="['csv', 'excel', 'ods', 'pdf', 'rtf', 'xml']"/>

and in my controller:

    if(params?.format && params.format != "html"){
    response.contentType = grailsApplication.config.grails.mime.types[params.format]
    response.setHeader("Content-disposition", "attachment; filename=books.${params.extension}")
        exportService.export(params.format, response.outputStream,params.fruitInstanceList, [:], [:])
    }

The code doesnt work because the fruitInstanceList is not being pass to the controller. How can I do this? To pass the value from the gsp to controller using the above code?

Please help, thanks.

You don't have to pass your reporting data all the way back to the controller. You got this data (here 'fruitInstanceList') from the controller, maybe based on some filter options. All you need to do is POST filter options again to the controller which will then fetch the same data (ie 'fruitInstanceList') and then export it to your desired format. The skelton code would look something like following:

reportFilter.gsp

<form name="fruitsFilterForm" action="fruitsReport" controller="report">
    <input type="text" name="search" value="${params.search}" />
    <input type="submit" value="Show Report" />
</form>

fruitsReport.gsp

<form name="reportExportForm" action="exportFruitsReport" controller="report">
    <input type="text" name="search" value="${params.search}" />
    <input type="hidden" name="format" value="${defaultFormat}" />
    <input type="submit" value="Export" />
</form>

ReportController.groovy

def reportService
def fruitsReport() {
    def fruitInstanceList = reportService.fetchFruitsList(params);
    [fruitInstanceList: fruitInstanceList, params: params, defaultFormat: "excel"]
}

def exportFruitsReport() {
    def fruitInstanceList = reportService.fetchFruitsList(params);

    /** All your code to export the fetched fruitInstanceList **/        
}

ReportService.groovy

def fetchFruitsList(params) {
    return Fruit.findAll(/* your query based on the params */)
}

NOTE

If you still insist to pass the data from gsp to controller instead then you can still do it by POSTing your list with an ajax call. (but that would be a dirty approach)

This variable is only available to the page. you may to pass it using params

 <export:formats formats ="[]", params ="[fruitInstanceList:${fruitInstance}]"/>

You cannot pass list as parameter from views. Since, the params will go to controller as get, the parameter will concatenate to the URL and it will convert as string.

So what you want to do is, you need to do the logic in that action where you use export code.

ie, How you are getting the list in controller and sending it to views? like the same way, here you have to do some business logic and get the list and then send it to export plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM