我有一个带有三个按钮的表单,一个用于保存、更新和查询,但是我想在服务器端知道用户单击了哪个按钮。我尝试使用 request.getParameter("action") 和 request.getAttribute("action") 但它们都返回 null。有什么方法可以在服务器上获取它,也许我可以在会话中存储“动作”值?如果是这样,我如何创建和存储会话变量?
代码
<button class="btn" value="save" id="action"></button>
<button class="btn" value="update" id="action"></button>
<button class="btn" value="query" id="action"></button>
基本上我试图避免重新发布到服务器。我知道我想使用 POST/redirect/GET 模式,但是我的方法不支持重定向,即使更改并且表单有错误,我也无法返回服务器验证。
控制器
@RequestMapping(value = "crime_registration_save.htm", method = RequestMethod.POST)
public ModelAndView handleSave(@Valid @ModelAttribute Crime crime,HttpServletRequest request,
HttpServletResponse response,BindingResult result, ModelMap m, Model model) throws Exception {
String action = request.getParameter("action");
logger.info("The requested action is "+ action);
if (result.hasErrors()) {
logger.debug("Has Errors In crime_registration_save");
model.addAttribute("dbcriminals", myCriminalList);
model.addAttribute("dbvictims", myVictimList);
model.addAttribute("status", myStatusList);
model.addAttribute("crimeCategory", myCrimeCategoryList);
model.addAttribute("crimeLevel", myCrimeLevelList);
model.addAttribute("officers", myOfficerList);
model.addAttribute("victimList", crime.getVictims());
model.addAttribute("criminalList", crime.getCriminals());
model.addAttribute("crimeTypeList",
crimeTypeManager.getCrimeTypeList(crime.getOffenceCatId()));
model.addAttribute("icon", "ui-icon ui-icon-circle-close");
model.addAttribute("results", "Error: Unable to Save Record!");
return new ModelAndView("crime_registration");
}
logger.debug("No errors going to preform save");
int crimeRecNo;
crimeRecNo = crimeManager.saveCrime(crime);
model.addAttribute("dbcriminals", myCriminalList);
model.addAttribute("dbvictims", myVictimList);
model.addAttribute("status", myStatusList);
model.addAttribute("crimeCategory", myCrimeCategoryList);
model.addAttribute("crimeLevel", myCrimeLevelList);
model.addAttribute("officers", myOfficerList);
model.addAttribute("save", "disabled");
model.addAttribute("victimList", crime.getVictims());
model.addAttribute("criminalList", crime.getCriminals());
model.addAttribute("crimeTypeList",
crimeTypeManager.getCrimeTypeList(crime.getOffenceCatId()));
model.addAttribute("crimeRecNo", crimeRecNo);
model.addAttribute("crimeRecordNoStatus", "true");
model.addAttribute("icon", "ui-icon ui-icon-circle-check");
model.addAttribute("results", "Record Was Saved");
return new ModelAndView("crime_registration");
}
jQuery
function submitPage(urlMapping,method,action) {
alert(urlMapping);
document.getElementById("crime_registration").action = urlMapping;
document.getElementById("crime_registration").target = "_self";
document.getElementById("crime_registration").method = method;
document.getElementById("crime_registration").submit();
$('#action').val(action);
alert($('#action').val());
return false;
}