gpt4 book ai didi

java - Spring MVC HttpServletRequest请求: which button clicked on

转载 作者:行者123 更新时间:2023-12-01 18:49:53 25 4
gpt4 key购买 nike

我有一个带有三个按钮的表单,一个用于保存、更新和查询,但是我想知道在服务器端用户单击了哪个按钮。我尝试使用 request.getParameter("action") 和 request.getAttribute("action") 但它们都返回 null。有什么方法可以在服务器上获取此内容,也许我可以在 session 中存储“操作”值?如果是这样,我如何创建和存储 session 变量?

代码

<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 模式,但是我的方法不支持重定向,即使更改并且表单有错误,我也将无法返回服务器验证。

Controller

@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;
}

最佳答案

表单内容不是请求属性,而是参数。

使用提交,为其命名,然后通过该名称检索参数。

<input type="submit" class="btn" value="save" name="action" />

String button = request.getParameter("action");

如果它不在表单中,您将需要一些 JavaScript,但您的问题暗示它是。

关于java - Spring MVC HttpServletRequest请求: which button clicked on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com