如何在grails中读取xml文件?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,我将 xml 文件放在
webapp/xmls/
目录下,并解析abc.xml
文件这是示例 XML (abc.xml) 文件:
希望这会有所帮助..
Here I am placing my xml files under
webapp/xmls/
directory, and parsingabc.xml
fileThis is the sample XML (abc.xml) file:
Hope this will help ..
这是一个快速示例。
控制器
视图 (index.gsp)
Here is a quick sample.
Controller
View (index.gsp)