【发布时间】:2020-05-01 00:49:05
【问题描述】:
如果服务器响应的状态失败,我正在尝试将我的请求重定向到编辑页面。 下面的代码将使我们更加清晰。由于协议更新了变量名称和 id。
控制器:
@Controller
@RequestMapping("abc")
public class CredentialsController {
@RequestMapping(value = "/editCredentials", method = RequestMethod.POST)
public String editCredentials(
@ModelAttribute(value = "credential")
Credentials credential, HttpServletRequest request, Model model
) throws Exception {}
@RequestMapping(value = "/update/{abc}", method = {RequestMethod.POST, RequestMethod.GET})
public ModelAndView update (
@Valid Form cForm, BindingResult bindResult,@PathVariable String abc,HttpServletRequest request, Model model
) throws Exception {
if(response.getMessageStatus().getStatus().equalsIgnoreCase("fail")){
model.addAttribute("response",response);
//TODO: redirect to edit page
return new ModelAndView("redirect:/app/editcredentials");
}
}
}
HTML:
<form name="carrier" id="carrierId" action="#" th:action="@{/app/update/} + ${abc}" th:object="${cForm}" method="post">
<div class="column-60" style="margin-left: auto; margin-right: auto; float: none;">
<p class="form-header">Edit Form - <span th:text = ${Name}></span>
.....
.....
<input type="submit" id="subBtn" class="button-orange" value="Save">
</form>
上面的代码没有重定向到编辑页面,我尝试在控制器“editcredentials”中返回字符串但没有加载页面。 注意:我在编辑页面并保存数据,如果服务器响应失败,我必须弹出响应消息并留在编辑(相同)页面上。 如果我们有任何使用 Jquery 和 Javascript 的解决方案也可以。 我还搜索了如何重定向到 GET 到 POST 并尝试但没有运气。 谢谢
【问题讨论】:
-
我在提交之前尝试使用 jquery:
event.preventDefault(); var req = $.ajax({ url: getContextPath() + "/app/update/{abc}", type: 'POST', data: getFormData($( "form[name='carrierCreds']").serializeArray()) }); req.done(function(data) { console.log(data); }); form.submit();但没有工作。
标签: javascript jquery html spring-boot thymeleaf