我有一个带有三个按钮的表单,一个用于保存,更新和查询,但是我想在服务器端知道用户点击了什么按钮。我尝试使用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;
}
答案 0 :(得分:2)
表格不是请求属性,而是参数。
使用提交,为其命名,并按该名称检索参数。
<input type="submit" class="btn" value="save" name="action" />
String button = request.getParameter("action");
如果表单中的不是,您需要一些JavaScript,但您的问题暗示它是。
答案 1 :(得分:1)
这里的解决方案是有一个名为action
的隐藏变量,点击该按钮,您可以使用单击按钮的值更新隐藏变量,然后提交表单。
例如:
<form action="" method="post" name="myform">
<input type="hidden" name="action" />
....
<button class="btn" value="save" id="action" onclick="submitForm(this)"></button>
<button class="btn" value="update" id="action" onclick="submitForm(this)"></button>
<button class="btn" value="query" id="action" onclick="submitForm(this)"></button>
</form>
然后
<script>
function submitForm(btn){
var frm = document.myform;
frm.action.value = btn.value;
frm.submit();
return false;
}
</script>
答案 2 :(得分:1)
您需要添加到按钮的所有内容是属性type =“submit”和属性name =“parametername”。然后按钮标签就像提交类型的输入一样,其优点是值可以与显示文本或按钮标签内的任何内容不同。
如果你想用ajax提交你的表单我现在会使用jquery ajaxform插件来处理这个问题。