在我的 crud spring-mvc 中发送消息

发布于 2024-09-13 02:47:26 字数 406 浏览 1 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(1

温柔戏命师 2024-09-20 02:47:26

当您使用 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 to myObject keyed by a String derived from the name of the class of myObject.

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 for statusMessage on the model: <c:out value="${statusMessage}" />

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