简体   繁体   English

Spring MVC-如何将数据从jsp页面的按钮传递到Controller?

[英]Spring MVC - How to pass data from jsp page's button to Controller?

I have a line of code like this in the jsp: 我在jsp中有一行这样的代码:

<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>

And in my Controller I use: 在我的控制器中,我使用:

@RequestParam String CurrentDelete

I am trying to pass the value of ${ra_split} into the Controller when I hit the Delete button, but all I am getting is the value of the text 'Delete' instead. 当我按下Delete按钮时,我试图将$ {ra_split}的值传递到Controller中,但是我得到的只是文本'Delete'的值。 Why is that? 这是为什么?

Here's the explanation 这是解释

If you use the element in an HTML form, Internet Explorer, prior version 8, will submit the text between the and tags, while the other browsers will submit the content of the value attribute. 如果您以HTML形式使用元素,则Internet Explorer(先前版本8)将在和标记之间提交文本,而其他浏览器将提交value属性的内容。

By returning to this issue after a few days I figured out a solution. 几天后回到这个问题,我想出了一个解决方案。

Just use: 只需使用:

<input type="hidden" value="${ra_split}" name="CurrentDelete">
<input type="submit" value="Delete" />

instead of: 代替:

<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>

Then the problem will be solved and the String CurrentDelete will contain the value ${ra_split} instead of the text 'Delete'. 然后将解决问题,并且字符串CurrentDelete将包含值$ {ra_split}而不是文本“ Delete”。

Extra information I have got when trying to solve the problem: 尝试解决此问题时,我得到了更多信息:

The button tag: 按钮标签:

<button name="CurrentDelete" value="${ra_split}" type="submit">Delete</button>

Will always pass the value between the button tags to the Controller (in this case the text 'Delete') instead of passing the value="${ra_split}". 始终将按钮标签之间的值传递给控制器​​(在这种情况下为文本“删除”),而不是传递值=“ $ {ra_split}”。

Either using 要么使用

HttpServletRequest req 

in the Controller and then do: 在控制器中,然后执行以下操作:

String CurrentDelete = req.getParameter("CurrentDelete");

or using 或使用

@RequestParam String CurrentDelete 

in the Controller, 在控制器中

would both get the same result. 两者都会得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM