0

I have a Grails project and I need to send/save table data. I have a controller(doesn't have views) with following code.

class JsonController {

def getCompany = {
    for (String s in request.getHeaderNames()) {

        println request.getHeader(s)
    }
    println Company.list()
    render Company.list() as XML
}

def getEmployees = {
    for (String s in request.getHeaderNames()) {

        println request.getHeader(s)
    }
    render Employees.list() as XML
}

def getManagers = {
    for (String s in request.getHeaderNames()) {

        println request.getHeader(s)
    }
    render Managers.list() as XML
}
}

Now I need to call/run these functions on clicking an link and send the output thru email or save to a folder. How can I do this?

Thankyou

4

2 回答 2

1

注意到您有电子邮件文本文件的答案 - 这是如何存储到 XML 文件,其中 rowid 将是每个 xml 行的定义

def file=""${System.properties['catalina.base']}/file.xml" 
try {
          new File(file).withWriter { writer ->

          def xml = new MarkupBuilder( writer )
          def Users = Registeration.list()
          xml.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
          xml.rowid {
            Employees.each { employee ->
               xml.registeration(id: employee.id) {
                                   username(employee.username)
                                   //somethingelse(employee.somethingelse)
              }
               }
           }
             }

      } catch (Exception e) {
          result=e.printStackTrace()
      }
      if (result!=null) { 
           result="all done stored in "+file
      }else{
          result="Something has gone wrong with "+file
      }
于 2013-09-23T14:58:52.303 回答
1

注意:以下代码需要邮件插件

我会将您JsonController移至一项服务,该服务已重命名为XMLService,因为这就是您要返回的内容。然后,将其注入XMLService您想在其中使用它的任何工件,例如另一个服务或控制器。

import grails.converters.XML;

class XMLService {

  def getCompanies = {
    return Company.list() as XML
  }

  def getEmployees = {
    return Employees.list() as XML
  }

  def getManagers = {
    return Managers.list() as XML
  }
}

然后,在控制器或服务中。此示例使用服务:

  class MyMailingService {  

    def mailService  //<-- included from the Mail plugin
    def xmlService

    void sendEmployeeList(){
      mailService.sendMail {
        to "fred@g2one.com","ginger@g2one.com"
        subject "Hello to mutliple recipients"
        body xmlService.getEmployees()
      }
    }

  }

对于存储文件,如下所示。请注意,这不一定是服务,但出于演示目的,它更容易。

  class MyStorageService {  

    def xmlService

    void storeEmployeeList(){
      //It was a little unclear how you wanted to store the file, so be careful because this this does not include checks you would want to implement in production (e.g. checks for existing files, possible runtime exceptions, etc).
      def f= new File('employees.txt') //see http://docs.codehaus.org/display/GROOVY/JN2015-Files
      f << xmlService.getEmployees()
    }

  }
于 2013-09-23T14:56:06.877 回答