如何在grails中读取xml文件?

发布于 2024-11-08 01:52:22 字数 1440 浏览 0 评论 0原文

我对 grails 很陌生,也许这将是我问的最简单的问题。 我正在创建一个非常简单的自学应用程序,我在其中创建了一个登录页面。成功登录后,应读取 xml 文件并显示输出。任何人都可以用一个示例来说明这一点。另外请告诉 xml 文件的文件夹位置应该是什么?下面是我的代码: UserController.groovy

class UserController {

    def index = { }

    def login = {
        def user = User.findWhere(username:params['username'],
                                  password:params['password'])
                                  session.user = user
        if (user) {     
            redirect(action:display)
        }
        else {
             redirect(url:"http://localhost:8080/simple-login/")
        }
    }
    def display = {
        def stream = getClass().classLoader.getResourceAsStream("grails-app/conf/sample.xml")
        return [data: XML.parse(stream)]
    }

}

myxml.gsp

<html>
<body>
    <p>Please find the  details below:</p>
    <p>${data}</p> 
</body>
</html>

URLMappings.groovy

class UrlMappings {

    static mappings = {
        "/user/login" (controller: "user" ,action: "login")
        "/user/display"(controller:"user" ,action:"display")

        "/"(view:"/index")
        "500"(view:'/error')
    }

}

现在我已经将index.gsp作为用户登录时显示的第一个页面了,是吗?是否可以在 URLMappings 中指定多个视图?另外,正如其中一个回复中所建议的,如果我必须定义一个名为“myxml”的操作并定向到诸如“/controller”/myxml 之类的 url,那会在哪里?请帮忙!

I am very new to grails and perhaps it would be the most simplest of questions that I am asking.
I am creating a very simple application for self-learning where I created a login page. On successful login,the xml file should be read and the output should be displayed. Can anyone please illustrate this with a sample example. Also please tell what should be the folder location for the xml file?Below is my code:
UserController.groovy

class UserController {

    def index = { }

    def login = {
        def user = User.findWhere(username:params['username'],
                                  password:params['password'])
                                  session.user = user
        if (user) {     
            redirect(action:display)
        }
        else {
             redirect(url:"http://localhost:8080/simple-login/")
        }
    }
    def display = {
        def stream = getClass().classLoader.getResourceAsStream("grails-app/conf/sample.xml")
        return [data: XML.parse(stream)]
    }

}

myxml.gsp

<html>
<body>
    <p>Please find the  details below:</p>
    <p>${data}</p> 
</body>
</html>

URLMappings.groovy

class UrlMappings {

    static mappings = {
        "/user/login" (controller: "user" ,action: "login")
        "/user/display"(controller:"user" ,action:"display")

        "/"(view:"/index")
        "500"(view:'/error')
    }

}

Now that I already have index.gsp as the first page that appears when user login, is it possible to specify more than one view in URLMappings? Also as suggested in one of the replies, if I have to define an action named "myxml" and direct to a url such as "/controller"/myxml where would that be? Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-11-15 01:52:22

在这里,我将 xml 文件放在 webapp/xmls/ 目录下,并解析 abc.xml 文件

def parse ( ) {
  // Getting context path here
  def webRootDir = sch.servletContext.getRealPath ("/")

  // Create a new file instance
  def f = new File (webRootDir + "/xmls/" + "abc.xml")

  // Parxing XML file here
  def items = new XmlParser ( ).parseText( f.text )

  // iterating through XML blocks here
  items.question.each {
        // Creating domain class object to save in DB
    def question = new Question ( )
    def node = it
    question.with {
    qtext = node.qtext.text()
    answer = node.answer.text()
    if (!hasErrors() && save(flush: true)) {
      log.info "mcq saved successfully"
    } else
    errors.allErrors.each {
          err->
              log.error err.getField() + ": "
              log.error err.getRejectedValue() + ": " + err.code
    }
   }
 }
}

这是示例 XML (abc.xml) 文件:

<qns>
  <question id="q1">
    <qtext> First letter of alphabet is?</qtext>
    <answer>A<answer>
  </question>

  <question id="q2">
    <qtext> Second letter of alphabet is?</qtext>
    <answer>A<answer>
  </question>
  .
  .
  .
  .
</qns>

希望这会有所帮助..

Here I am placing my xml files under webapp/xmls/ directory, and parsing abc.xml file

def parse ( ) {
  // Getting context path here
  def webRootDir = sch.servletContext.getRealPath ("/")

  // Create a new file instance
  def f = new File (webRootDir + "/xmls/" + "abc.xml")

  // Parxing XML file here
  def items = new XmlParser ( ).parseText( f.text )

  // iterating through XML blocks here
  items.question.each {
        // Creating domain class object to save in DB
    def question = new Question ( )
    def node = it
    question.with {
    qtext = node.qtext.text()
    answer = node.answer.text()
    if (!hasErrors() && save(flush: true)) {
      log.info "mcq saved successfully"
    } else
    errors.allErrors.each {
          err->
              log.error err.getField() + ": "
              log.error err.getRejectedValue() + ": " + err.code
    }
   }
 }
}

This is the sample XML (abc.xml) file:

<qns>
  <question id="q1">
    <qtext> First letter of alphabet is?</qtext>
    <answer>A<answer>
  </question>

  <question id="q2">
    <qtext> Second letter of alphabet is?</qtext>
    <answer>A<answer>
  </question>
  .
  .
  .
  .
</qns>

Hope this will help ..

凉月流沐 2024-11-15 01:52:22

这是一个快速示例。

控制器

def index = {
  def stream = getClass().classLoader.getResourceAsStream("grails-app/conf/my-file.xml")
  return [data: XML.parse(stream)]
}

视图 (index.gsp)

<html>
...
<body>
  <p>${data}</p>
</body>
</html>

Here is a quick sample.

Controller

def index = {
  def stream = getClass().classLoader.getResourceAsStream("grails-app/conf/my-file.xml")
  return [data: XML.parse(stream)]
}

View (index.gsp)

<html>
...
<body>
  <p>${data}</p>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文