spring mvc 重定向,地址栏带了参数,不想带怎么办
1个回答
展开全部
在spring mvc
3.1后,可以这样
@RequestMapping(value = "/user/save", method = RequestMethod.POST)
public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes)
throws Exception {
redirectAttributes.addFlashAttribute("message", "保存用户成功!");//使用addFlashAttribute,参数不会出现在url地址栏中
return "redirect:/user/save/result";
}
来个稍微完整点的例子,首先是一个表单,在其中填入一些信息:
<form:form id="myform" action="saveUserDetails.action" method="POST" commandName="user">
<form:input type="text" name="firstName" path="firstName"/>
<form:input type="text" name="lastName" path="lastName"/>
<form:input type="text" name="email" path="email"/>
<input type="submit" value="submit">
</form:form>
则在controller中,可以这样:
@RequestMapping(value="/saveUserDetails.action",method=RequestMethod.POST)
public String greetingsAction(@Validated User user,RedirectAttributes redirectAttributes){
someUserdetailsService.save(user);
redirectAttributes.addFlashAttribute("firstName",user.getFirstName());
redirectAttributes.addFlashAttribute("lastName",user.getLastName())
return"redirect:success.html";
}
success.html:
<div>
<h1>Hello ${firstName} ${lastName}. Your details stored in our database.</h1>
</div><br>
但如果F5的时候,会发现参数丢失,因为flash scope其实只支持redirect的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询