简体   繁体   中英

Using jQuery/javascript to submit/POST to Spring controller without hidden input

I was wondering if it's possible submit/POST with a Spring param for a RequestMapping using javascript/jQuery without using a hidden input field. I have looked at other answers, like using javascript to add a hidden field, but if possible I'd like to avoid that totally. My jsp where I'm submitting from looks like below, but not exactly, I've modified variable names, etc.

<form:select class="selectpicker" title="Copy" data-style="btn-success" path="copy" onchange="$('#formId').submit()" data-width="35%">
    <form:option value="0">Copy To This</form:option>
    <form:option value="1">Copy To Other</form:option>
</form:select>

And this is the RequestMapping I'd like it to go to.

@RequestMapping(params="copy", method = RequestMethod.POST)
public ModelAndView copy(@ModelAttribute("SpringWeb") Object object) throws SQLException{
        // code here

        return new ModelAndView("form", "command", object);
}

I've tried several different things but so far with no success. Like adding name="copy" to the form:select with onchange=""$(#formId).attr('name', 'copy').submit(); .

I did a decent amount of research and couldn't find a way using javascript to submit/POST with Spring param. Instead I just used different submits with requestMappings. See example below.

<div class="dropdown inline-div">
<button name="copy" class="btn btn-success dropdown-toggle" data-toggle="dropdown"
    aria-haspopup="true" aria-expanded="false">Copy<span class="caret"></span></button>
 <ul class="dropdown-menu">
    <li><a><input type="submit" name="copy1" value="Copy To 1"></a></li>
    <li><a><input type="submit" name="copy2" value="Copy to 2"></a></li>
    <li><a><input type="submit" name="copy3" value="Copy to 3"></a></li>
    <li><a><input type="submit" name="copy4" value="Copy to 4"></a></li>
 </ul></div>

With a request mapping like this for each of the names:

@RequestMapping(params="copy1", method = RequestMethod.POST)
public ModelAndView copyToQ1(@ModelAttribute("SpringWeb") Object object) throws SQLException{       
    String id = object.getQuarterDetails().get(FINAL_1).getQuarter().getqId();

    if(copyItems(object, id)){
        logger.info("Items successfully copied for user: " + object.getPerson());
    } else {
        logger.error("Items have not been copied for user: " + object.getPerson());
    }
    object = service.loadForm(object.getEmployee());
    return new ModelAndView("form", "command", object);
}

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