在 Grails 控制器的渲染方法中设置编码

发布于 2024-09-03 20:09:58 字数 1668 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

终陌 2024-09-10 20:09:58

'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?

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