基于Spring MVC的网站上的状态信息(注解控制器) [英] Status messages on the Spring MVC-based site (annotation controller)
问题描述
什么是使用注释控制器基于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屋!