java 将对象数据从 JSP 传递到 Spring Controller Post 方法而不是 GET 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27010706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:03:39  来源:igfitidea点击:

Passing an object data from JSP to Spring Controller Post Method instead of GET method

javaspringjspspring-mvc

提问by swagath001

Am new to Spring Web MVC and developing a web application. There is a case where i need to pass a data value to Controller. (Note: Here the data value is a value of bean object )

我是 Spring Web MVC 的新手并正在开发 Web 应用程序。有一种情况我需要将数据值传递给控制器​​。(注意:这里的数据值是一个bean对象的值)

Item Number  Item Name  Description  Price

 Item1018     Item1      Desc1        5.0      Add item to Cart  
 Item1019     Item2      Desc2        2.0      Add item to Cart  

As shown in above image, if i click on "Add item to Cart" the respective item number should be passed to controller.

如上图所示,如果我点击“将项目添加到购物车”,相应的项目编号应传递给控制器​​。

viewmenu.jsp

视图菜单.jsp

<c:forEach items="${model.itemlists}" var="item">
<tr>
   <c:if test = "${item.status == 'available'}">   
    <td><c:out value="${item.itemNo}"/>  </td>
    <td><c:out value="${item.itemName}"/></td>
    <td><c:out value="${item.description}"/></td>
    <td><c:out value="${item.price}"/></td>
    <td><a href="<c:url value="additemtocart">
             <c:param name='itemNumber' value="${item.itemNo}"/>
         </c:url>">Add item to Cart</a> </td>
    </c:if>
</tr>
</c:forEach>

CustomerController.java method

CustomerController.java 方法

@RequestMapping(value = "additemtocart",method = RequestMethod.GET)
     public ModelAndView addItemToCart(@RequestParam("itemNumber") String itemno  ) throws ClassNotFoundException, SQLException {
                   System.out.println("Username test in customer controller: "+userName);

 }

The code works and am able to pass the value. But as per standards of GET and POST methods (described here), i think am doing wrong. Please suggest is there any other way to pass the data in to the POST method. Please suggest me. Thanks in advance

该代码有效并且能够传递该值。但是根据 GET 和 POST 方法的标准(此处描述),我认为我做错了。请建议是否有任何其他方式将数据传递给 POST 方法。请建议我。提前致谢

(This is first time am posting here, excuse me if there are any mistakes )

(第一次发帖,如有错误请见谅)

采纳答案by Neil McGuigan

Put everything inside a form (using Spring's <form:form>tag). This will POST the form to the same URL at which it is displayed. If you want to POST to a different URL, then set the actionattribute of the form.

把所有东西都放在一个表单中(使用 Spring 的<form:form>标签)。这会将表单 POST 到显示它的相同 URL。如果要 POST 到不同的 URL,则设置action表单的属性。

Change your Add item to Cartlink to a submit button, as you should not change data with a GET. You can style it as you wish and make it look like a link if you want.

将您的Add item to Cart链接更改为提交按钮,因为您不应使用 GET 更改数据。您可以根据需要设置样式,并根据需要使其看起来像链接。

Your button should look like this:

您的按钮应如下所示:

<button type=submit name=itemNumber value=xxx>Add item to Cart</button>

In your controller, you should have a handler method with a request mapping like this:

在您的控制器中,您应该有一个带有请求映射的处理程序方法,如下所示:

@RequestMapping(value="additemtocart", method=RequestMethod.POST, params={"itemNumber"})

You should then use the Post-Redirect-Get pattern to redirect back to the product list. See flashAttributes in Spring MVC documentation.

然后,您应该使用 Post-Redirect-Get 模式重定向回产品列表。请参阅 Spring MVC 文档中的 flashAttributes。

回答by M Sach

Yes you are correct you should use POST request to do any kind of DML operation on server. GET should be used for data fetch operations only. Reason is to avoid the doubly form submission which you can do with F5 or double click of submit button. You should use method=RequestMethod.POST instead of GET. See Spring MVC Post Request

是的,您是对的,您应该使用 POST 请求在服务器上执行任何类型的 DML 操作。GET 应该仅用于数据获取操作。原因是为了避免使用 F5 或双击提交按钮可以进行的双重表单提交。您应该使用 method=RequestMethod.POST 而不是 GET。请参阅Spring MVC 发布请求

回答by Shoaib Chikate

You have add to cart button so on clicking on it you can have Ajax call and use appropriate request mapping in controller.

您已经添加到购物车按钮,因此单击它您可以调用 Ajax 并在控制器中使用适当的请求映射。

<a href="javascript:addItemToCart(${item.itemNo},"passURL");">Add to cart item?</a>

In Javascript, you can pass parameter and give type as POST for ajax request.

在Javascript中,您可以传递参数并为ajax请求提供类型为POST。

JavaScript code

JavaScript 代码

function addItemToCard(itemNo,targetURL){

  $.ajax(function(){
    url:targetURL,
    type:"POST",
    data:"itemNumber":itemNo,
    success:function(response){
       alert("Added successfully");
    }
  });

}