在我的 crud spring-mvc 中发送消息
我正在学习 spring-mvc 并且正在做一个简单的处理。
我有一个项目列表,上面有一个大的添加按钮。 当用户单击添加时,它将重定向到 /item/add
,其中视图是一个表单。
现在,当用户加载新项目时,我想在列表中显示一条消息,内容如下:
“项目添加成功”
我注意到我可以做类似的事情:
If ( noErrors ) {
model.addAttribute("Item added successfully");
return new ModelAndView("redirect:/item", model);
}
但我没能让它工作。
有什么想法吗?
I am learning spring-mvc
and I am doing a simple crud.
I have a list of items with a big add button on top of it.
When the user clicks add it will redirect to /item/add
where the view is a form.
Now, when the user loads a new item I want to show a msg in the list saying something like:
"Item added successfully"
I noticed that I can do something like:
If ( noErrors ) {
model.addAttribute("Item added successfully");
return new ModelAndView("redirect:/item", model);
}
But I didn't manage to get it working.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
model.addAttribute(myObject)
时,按照惯例,您在模型中创建对myObject
的引用,该引用由从名称派生的String
进行键控myObject
类的。例如,如果我添加
MyUser
类的实例:model.addattribute(myUserInstance)
,那么我将能够通过键 <代码>“我的用户”。当您的对象是字符串时,事情会变得棘手,因为没有明显的类可用于生成模型中的键。
尝试改为指定您自己的密钥:
model.addAttribute("statusMessage", "item added success")
。然后在您的视图中,您只需通过在模型上查找statusMessage
来访问该对象:When you use
model.addAttribute(myObject)
, by convention you create a reference in the model tomyObject
keyed by aString
derived from the name of the class ofmyObject
.For example, if I add an instance of the
MyUser
class:model.addattribute(myUserInstance)
, then I will be able to access that object on the model by the key"myUser"
.Things get tricky when your object is a String, because there's no obvious class to use to generate the key in the model.
Try instead specifying your own key:
model.addAttribute("statusMessage", "item added successfully")
. Then in your view, you simply access the object by looking forstatusMessage
on the model:<c:out value="${statusMessage}" />