RedirectAttributes使用~~ 成功外掛:class="alert alert-success"
1、java程式碼
@RequestMapping("/add.do")
public String add(@ModelAttribute("adminCreate") @Validated AdminCreate adminCreate,
BindingResult errors, RedirectAttributes attr,HttpServletRequest request) {
if (errors.hasErrors()) {
return "/admin/staff/create";
}
// 如果密碼輸入了
String password = adminCreate.getPassword();
String passwordConfirm = adminCreate.getPwd();
if (!password.equals(passwordConfirm)) {
errors.rejectValue("pwd", "", "請輸入相同密碼");
return "/admin/staff/create";
}
//check使用者名稱
if (adminStaffCreateService.checkStaffnameRepeat(adminCreate.getStaffname())) {
errors.rejectValue("staffname", "", "使用者名稱重複");
return "/admin/staff/create";
}
//插入資料,
adminStaffCreateService.add(adminCreate);
//返回資訊,當然之前要做插入成功的判斷,
attr.addFlashAttribute("commonmessage2", "員工新建成功!");
// 返回上級頁面
return "redirect:/staff/list/index.action";
}
2、index,jsp頁面程式碼:
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-lg-12">
<h2><st:message code="staff.title"></st:message><st:message code="common.list"></st:message></h2>
<c:if test="${not empty commonmessage2}">
<div id="myAlert" class="alert alert-success">
<a href="#" class="close" data-dismiss="alert">×</a>
//此處顯示:"員工新建成功!"
<strong>${commonmessage2}</strong>
</div>
</c:if>
</div>
</div>
3、js程式碼:-------似乎沒有用,不寫它頁面照樣可以顯示"員工新建成功!"
<c:if test="${not empty commonmessage}">
$('#myAlert').alert();
</c:if>