我有一个spring MVC表单,它在POST时处理错误并在表单上正确显示它们(在同一页面上显示错误并且不清除表单中的数据)。我还想在成功交易时在表单上添加成功消息,并清除表单内容。
我对如何处理成功消息一无所知。我尝试将它作为错误传递给表单,但是它以红色显示,这是我用于错误的颜色。我无法将此消息的颜色更改为绿色。
model.addAttribute("serverError", "Transaction inserted Successfully !");
return "checkinout";
这是我的代码 -
.css
.plErroMessage{margin-left:185px;color:white;background-color:red;width:165px;padding: 4px;}
checkinout.jsp
<div class="devices">
<form:form method="post" id="assetForm" modelAttribute="inOutTransaction" action="/DeviceManager/assets">
<form:errors path="*" cssClass="plErroMessage" element="div" />
<br>
<c:if test="${not empty serverError}">
<div id="serverError" class="plErroMessage">${serverError}</div>
</c:if>
<form:hidden path="actionId"/>
<div>
<div class="plLabelSearch">User Id:</div>
<div class="plinput"><form:input path="userId" size="29"/></div>
</div> <!-- More Code follows -->
控制器
@RequestMapping(value="/assets", method = RequestMethod.POST)
public String checkInOut(@ModelAttribute("inOutTransaction") @Validated InOutTransaction inOutTransaction, BindingResult result, Model model, Locale locale) {
try {
if(result.hasErrors()){
return "checkinout";
}
InOutTransactionDO inOutTransactionDO = new InOutTransactionDO();
/*CODE*/
if(employeeInfoDO == null){
model.addAttribute("serverError", "User "+ inOutTransaction.getUserId()+" does not exist !");
return "checkinout";
}
/*CODE*/
if(deviceMasterDO == null){
model.addAttribute("serverError", "Device Id "+ inOutTransaction.getAssetTagNumber()+" not found !");
return "checkinout";
}
inOutTransactionService.saveTransaction(inOutTransactionDO);
model.addAttribute("serverError", "Transaction inserted Successfully !");
return "checkinout";
} catch (Exception e) {
model.addAttribute("serverError", e.toString());
return "checkinout";
}
}
答案 0 :(得分:0)
可能有更好的解决方案,但我过去一直想到这个。
1)在参数
中添加HttpServletRequest请求@RequestMapping(value="/assets", method = RequestMethod.POST)
public String checkInOut(@ModelAttribute("inOutTransaction") @Validated InOutTransaction inOutTransaction,
BindingResult result, Model model, Locale locale, HttpServletRequest request) {
try {
if(result.hasErrors()){
return "checkinout";
}
InOutTransactionDO inOutTransactionDO = new InOutTransactionDO();
/*CODE*/
if(employeeInfoDO == null){
model.addAttribute("serverError", "User "+ inOutTransaction.getUserId()+" does not exist !");
return "checkinout";
}
/*CODE*/
if(deviceMasterDO == null){
model.addAttribute("serverError", "Device Id "+ inOutTransaction.getAssetTagNumber()+" not found !");
return "checkinout";
}
inOutTransactionService.saveTransaction(inOutTransactionDO);
request.getSession().setAttribute("successMessage", "Transaction inserted Successfully !");
return "checkinout";
} catch (Exception e) {
model.addAttribute("serverError", e.toString());
return "checkinout";
}
}
2)将成功消息添加到jsp并在jsp
结束时清除会话变量<div class="devices">
<form:form method="post" id="assetForm" modelAttribute="inOutTransaction" action="/DeviceManager/assets">
<form:errors path="*" cssClass="plErroMessage" element="div" />
<c:if test="${not empty successMessage}">
<div id="serverError" class="successMessage">${successMessage}</div>
</c:if>
<br>
<c:if test="${not empty serverError}">
<div id="serverError" class="plErroMessage">${serverError}</div>
</c:if>
<form:hidden path="actionId"/>
<div>
<div class="plLabelSearch">User Id:</div>
<div class="plinput"><form:input path="userId" size="29"/></div>
</div>
<c:remove var="successMessage" scope="session" />
3)为成功消息创建新的css
4)要在成功时清除表单,请重定向页面或将新的空白对象发送到视图。
我在httpSession而不是model中设置成功消息的原因是,默认情况下,spring会在重定向时将模型属性附加到url上。如果您不想重定向并想要发送空白对象,那么添加到model.addAttribute也没关系。