简体   繁体   中英

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

I have a line of code like this in the 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. 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.

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'.

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}".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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