在post方法中接受参数" updateButton"和" reviewButton"。如果您只注册params = {"reviewButton"}
并点击" reviewButton",request.getParameter ("reviewButton") == reviewButton
,如果注册getParameter的两个参数返回空值。
如何确定按下哪个按钮?
页
<form action="update" name ="updateCourseDTO" " action='<@spring.url "/update" />' method="POST"/>
<fieldset>
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
<@spring.formInput "updateCourseDTO.name" ""/>
<@spring.showErrors '<br>',"error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Category</label>
<div class="controls">
<@spring.formSingleSelect "updateCourseDTO.category" listCategories "" />
<@spring.showErrors '<br>',"error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<@spring.formInput "updateCourseDTO.description"/>
<@spring.showErrors "updateCourseDTO.description","error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Links</label>
<div class="controls">
<@spring.formInput "updateCourseDTO.links"/>
<@spring.showErrors "updateCourseDTO.links","error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Minimal Subscribers</label>
<div class="controls">
<@spring.formInput "updateCourseDTO.minSubscribers" ""/>
<@spring.showErrors 'updateCourseDTO.minSubscribers',"error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Minimal Attende</label>
<div class="controls">
<@spring.formInput "updateCourseDTO.minAttendes" ""/>
<@spring.showErrors 'updateCourseDTO.minAttendes',"error" />
</div>
</div>
<div class="form-actions" >
<button id="updateButton" name="updateButton" class="btn btn-primary" type="submit" >Update</button>
<button id="reviewButton" name="reviewButton" class="btn btn-warning" type="submit" >Review</button>
</div>
</fieldset>
</form>
控制器
@RequestMapping(value = "/courses/{id}/update", method = RequestMethod.POST, params = { "updateButton",
"reviewButton" })
public String updateCoursePost(Model model, HttpSession session,
HttpServletRequest request, @PathVariable("id") Integer courseId,
@Valid UpdateCourseDTO updateCourseDTO, BindingResult result)
throws AddressException, Exception {
if (request.getParameter("updateButton") != null) {}
if (request.getParameter("reviewButton") != null) {}
答案 0 :(得分:1)
正如我从documentation看到的那样,params
正在缩小映射范围。因此,通过指定params = { "updateButton", "reviewButton" }
,只有当请求中存在两个参数时,才会告诉Spring映射方法,这是不可能的,因为无法同时单击两个按钮。所以,你要么省略params
捕捉方法中的两个按钮并在方法中做出选择:
@RequestMapping(value = "/courses/{id}/update", method = RequestMethod.POST)
public String updateCoursePost(Model model, HttpSession session,
HttpServletRequest request, @PathVariable("id") Integer courseId,
@Valid UpdateCourseDTO updateCourseDTO, BindingResult result)
throws AddressException, Exception
{
if (request.getParameter("updateButton") != null) {doUpdate(...);}
else if (request.getParameter("reviewButton") != null) {doReview(...);}
else {WHAAAT?!}
}
或者,正如我所希望的那样,做两种不同的方法:
@RequestMapping(value = "/courses/{id}/update", method = RequestMethod.POST, params = {"updateButton"})
public String updateCoursePost(Model model, HttpSession session,
HttpServletRequest request, @PathVariable("id") Integer courseId,
@Valid UpdateCourseDTO updateCourseDTO, BindingResult result)
throws AddressException, Exception
{
doUpdate(...);
}
@RequestMapping(value = "/courses/{id}/update", method = RequestMethod.POST, params = {"reviewButton"})
public String reviewCoursePost(Model model, HttpSession session,
HttpServletRequest request, @PathVariable("id") Integer courseId,
@Valid UpdateCourseDTO updateCourseDTO, BindingResult result)
throws AddressException, Exception
{
doReview(...);
}