刷新页面时避免Spring MVC表单重新提交 [英] Avoid Spring MVC form resubmission when refreshing the page

查看:99
本文介绍了刷新页面时避免Spring MVC表单重新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC将数据保存到数据库中.问题是刷新页面时它正在重新提交JSP页面. 以下是我的代码段

I am using spring MVC to save the data into database. Problem is it's resubmitting the JSP page when I am refreshing the page. Below is my code snippet

<c:url var="addNumbers" value="/addNumbers" ></c:url>
<form:form action="${addNumbers}" commandName="AddNumber" id="form1">

</<form:form>

@RequestMapping(value = "/addNumbers",  method = RequestMethod.POST)
public String addCategory(@ModelAttribute("addnum") AddNumber num){
    this.numSrevice.AddNumbers(num);
    return "number";
}

推荐答案

您必须实现获取重定向后.

完成POST方法而不是返回视图名称后,请使用"redirect:<pageurl>"发送重定向请求.

Once the POST method is completed instead of returning a view name send a redirect request using "redirect:<pageurl>".

@RequestMapping(value = "/addNumbers",  method = RequestMethod.POST)
public String addCategory(@ModelAttribute("addnum") AddNumber num){
    this.numSrevice.AddNumbers(num);
    return "redirect:/number";
}

然后使用method = RequestMethod.GET的方法返回视图名称.

And and have a method with method = RequestMethod.GET there return the view name.

@RequestMapping(value = "/number",  method = RequestMethod.GET)
public String category(){
    return "number";
}

因此post方法将向浏览器提供重定向响应,然后浏览器将使用get方法获取重定向网址,因为避免了重新提交

So the post method will give a redirect response to the browser then the browser will fetch the redirect url using get method since resubmission is avoided

注意:我假设您在控制器级别没有任何@RequestMapping.如果是这样,则在redirect:/numbers

Note: I'm assuming that you don't have any @RequestMapping at controller level. If so append that mapping before /numbers in redirect:/numbers

这篇关于刷新页面时避免Spring MVC表单重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆