在 Grails 控制器的渲染方法中设置编码
我正在尝试使用 Grails 和 Rome 构建 RSS 提要。
在我的控制器的 rss 操作中,我的最后一个命令是:
render(text: getFeed("rss_2.0"), contentType:"application/rss+xml", encoding:"ISO-8859-1 ")
但是,当我导航到提要的 URL 时,标题是:
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
...
我的 getFeed() 代码类似于:
def getFeed(feedType) {
def currentFeedURL = params.url
def items = parserService.parse(new URL(currentFeedURL))
def feedLink = "http://blablabla"
def feedEntries = []
items.each { item ->
def entryTitle
if (item.price != null)
entryTitle = item.description + " - " + item.price + " euros"
else
entryTitle = item.description
def itemContent = new SyndContentImpl(type:'text/plain', value: getBody(item))
SyndEntryImpl entry = new SyndEntryImpl(title: entryTitle,
link: item.link,
publishedDate: item.date,
description: itemContent)
feedEntries.add(entry)
}
def feed = new SyndFeedImpl(feedType: feedType,
encoding : "ISO-8859-1",
title: 'Some title',
link: 'http://acme.com',
description: 'Feed description',
entries: feedEntries)
StringWriter writer = new StringWriter()
SyndFeedOutput output = new SyndFeedOutput()
output.output(feed,writer)
writer.close()
return writer.toString()
}
我的 getBody(item) 只是解析一个项目并输出一些 HTML格式化文本。
有谁知道当我在渲染方法中将编码设置为 ISO-8859-1 时为什么编码是 UTF-8 ???
感谢您的帮助 !
I'm trying to build an RSS feed using Grails and Rome.
In my controller's rss action, my last command is :
render(text: getFeed("rss_2.0"), contentType:"application/rss+xml", encoding:"ISO-8859-1 ")
However, when I navigate to my feed's URL, the header is :
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
...
My code for getFeed() is something like :
def getFeed(feedType) {
def currentFeedURL = params.url
def items = parserService.parse(new URL(currentFeedURL))
def feedLink = "http://blablabla"
def feedEntries = []
items.each { item ->
def entryTitle
if (item.price != null)
entryTitle = item.description + " - " + item.price + " euros"
else
entryTitle = item.description
def itemContent = new SyndContentImpl(type:'text/plain', value: getBody(item))
SyndEntryImpl entry = new SyndEntryImpl(title: entryTitle,
link: item.link,
publishedDate: item.date,
description: itemContent)
feedEntries.add(entry)
}
def feed = new SyndFeedImpl(feedType: feedType,
encoding : "ISO-8859-1",
title: 'Some title',
link: 'http://acme.com',
description: 'Feed description',
entries: feedEntries)
StringWriter writer = new StringWriter()
SyndFeedOutput output = new SyndFeedOutput()
output.output(feed,writer)
writer.close()
return writer.toString()
}
And my getBody(item) is just parsing an item and outputting some HTML formatted text.
Does anyone have a clue about WHY the encoding is UTF-8 when I set it to ISO-8859-1 in the render method ???
Thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'getFeed("rss_2.0")' 是否可以从另一个源提取 xml?如果是这样,是否可以渲染该文件的编码而不是指定的渲染?
Could the 'getFeed("rss_2.0")' be pulling xml from another source? If so,could it be rendering that file's encoding instead of what render is specifying?