douliaopan1419 2019-01-26 15:58
浏览 32

陶醉的闪光消息不显示

I have created a revel web application with default skelton.

I have created a new route/action/view:

Controller action:

func (c Ctrl1) Action3() revel.Result {
    variable1 := "test1"
    variable2 := "test2"
    c.Flash.Error("Message")
    return c.Render(variable1,variable2)
}

Action3.html:

{{set . "title" "Test"}}
{{template "header.html" .}}
{{template "flash.html" .}}
Hello: {{.variable1}}
{{template "footer.html" .}}

The first time i have runned my webapp, i saw the flash message. But next times, if i refresh the page, it disapears !

I have restarted revel

Thanks

  • 写回答

2条回答 默认 最新

  • drny60365 2019-01-26 16:34
    关注

    I'm not familiar with revel, but message "flashing" is typically used when you need some user communication to carry over through a redirect. The godoc seems to describe it as exactly for that use case as well.

    If you're rendering a template directly in this request handler, you probably shouldn't be using c.Flash. My guess of what is happening is that revel will only show the flash message received with the request. Calling c.Flash.Error sets the field in the cookie, which means it will be sent back to the caller, not to the template. On the next render, it will read from the cookie that the caller sends back to the server, which would include this flash message. Apparently, though, setting a new flash message replaces the old one, causing it to (yet again) send it to the caller not the template.

    The good news is that there's really only one way for your message to get on the page, and you can almost definitely shoehorn your message there: the template data! Instead of calling c.Flash.Error, send the message using the usual mechanisms. In this case, assuming your flash.html template contains something like:

    {{ if .flash.error }}
        <div class="error">{{ .flash.error }}</div>
    {{ end }}
    

    you should be able to pass that data by replacing the c.Flash.Error("Message") line with:

    c.ViewArgs["flash"] = map[string]string{"error": "Message"}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿