基于Spring MVC的网站上的状态信息(注解控制器) [英] Status messages on the Spring MVC-based site (annotation controller)

查看:142
本文介绍了基于Spring MVC的网站上的状态信息(注解控制器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用注释控制器基于Spring MVC的网站上整理状态消息(您的资料已成功保存/添加/删除)的最好方法?

What is the best way to organize status messages ("Your data has been successfully saved/added/deleted") on the Spring MVC-based site using annotation controller?

所以,这个问题是在从位指示POST方法发送消息的方式。

So, the issue is in the way of sending the message from POST-method in contoller.

推荐答案

正如muanis提到的,由于春季3.1,最好的方法是使用RedirectAttributes。我加国际化,以在博客中给出的样本。因此,这将是一个完整的示例。

As mentioned by muanis, since spring 3.1, the best approach would be to use RedirectAttributes. I added i18n to the sample given in the blog. So this would be a complete sample.

@RequestMapping("/users")
@Controller
public class UsersController {

            @Autowired
            private MessageSource messageSource;

            @RequestMapping(method = RequestMethod.POST, produces = "text/html")
            public String create(@Valid User user, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, Locale locale, RedirectAttributes redirectAttributes) {
                ...
                ...
                redirectAttributes.addFlashAttribute("SUCCESS_MESSAGE", messageSource.getMessage("label_user_saved_successfully", new String[] {user.getUserId()}, locale));
                return "redirect:/users/" + encodeUrlPathSegment("" + user.getId(), httpServletRequest);
            }
    ...
    ...
}

添加相应的信息,在你的消息包,说messages.properties。

Add appropriate message in your message bundle, say messages.properties.

label_user_saved_successfully=Successfully saved user: {0}

编辑JSPX文件,以使用相关的属性。

Edit your jspx file to use the relevant attribute

<c:if test="${SUCCESS_MESSAGE != null}">
  <div id="status_message">${SUCCESS_MESSAGE}</div>
</c:if> 

这篇关于基于Spring MVC的网站上的状态信息(注解控制器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆