简体   繁体   中英

Spring MVC HttpServletRequest request: which button clicked on

I have a form with three buttons one for save, update and query however i would like to know on the server end what button the user clicked. I tried using request.getParameter("action") and request.getAttribute("action") but they both return null. Is there some way to get this on the server maybe i can store 'action' value in the session? If so how do i create and store session variables?

Code

<button class="btn" value="save"  id="action"></button>

<button class="btn" value="update"  id="action"></button>

<button class="btn" value="query"  id="action"></button>

Basically i am trying to avoid reposts to the server. I am aware that i am suppose to use POST/redirect/GET pattern however my method will not support redirect and even if changed and the form has errors i will not be able to return the server validation.

Controller

@RequestMapping(value = "crime_registration_save.htm", method = RequestMethod.POST)
    public ModelAndView handleSave(@Valid @ModelAttribute Crime crime,HttpServletRequest request,
            HttpServletResponse response,BindingResult result, ModelMap m, Model model) throws Exception {


        String action = request.getParameter("action");

    logger.info("The requested action is "+ action);
        if (result.hasErrors()) {

            logger.debug("Has Errors In crime_registration_save");
            model.addAttribute("dbcriminals", myCriminalList);
            model.addAttribute("dbvictims", myVictimList);
            model.addAttribute("status", myStatusList);
            model.addAttribute("crimeCategory", myCrimeCategoryList);
            model.addAttribute("crimeLevel", myCrimeLevelList);
            model.addAttribute("officers", myOfficerList);

            model.addAttribute("victimList", crime.getVictims());
            model.addAttribute("criminalList", crime.getCriminals());

            model.addAttribute("crimeTypeList",
                    crimeTypeManager.getCrimeTypeList(crime.getOffenceCatId()));
            model.addAttribute("icon", "ui-icon ui-icon-circle-close");
            model.addAttribute("results", "Error: Unable to Save Record!");

            return new ModelAndView("crime_registration");
        }
        logger.debug("No errors going to preform save");

        int crimeRecNo;

        crimeRecNo = crimeManager.saveCrime(crime);

        model.addAttribute("dbcriminals", myCriminalList);
        model.addAttribute("dbvictims", myVictimList);
        model.addAttribute("status", myStatusList);
        model.addAttribute("crimeCategory", myCrimeCategoryList);
        model.addAttribute("crimeLevel", myCrimeLevelList);
        model.addAttribute("officers", myOfficerList);
        model.addAttribute("save", "disabled");
        model.addAttribute("victimList", crime.getVictims());
        model.addAttribute("criminalList", crime.getCriminals());

        model.addAttribute("crimeTypeList",
                crimeTypeManager.getCrimeTypeList(crime.getOffenceCatId()));
        model.addAttribute("crimeRecNo", crimeRecNo);
        model.addAttribute("crimeRecordNoStatus", "true");
        model.addAttribute("icon", "ui-icon ui-icon-circle-check");
        model.addAttribute("results", "Record Was Saved");



        return new ModelAndView("crime_registration");
    }

jquery

function submitPage(urlMapping,method,action) {
        alert(urlMapping);
        document.getElementById("crime_registration").action = urlMapping;
        document.getElementById("crime_registration").target = "_self";
        document.getElementById("crime_registration").method = method;
        document.getElementById("crime_registration").submit();
        $('#action').val(action);

        alert($('#action').val());
        return false;
    }

Form things aren't request attributes, they're parameters.

Use a submit, give it a name, and retrieve the parameter by that name.

<input type="submit" class="btn" value="save" name="action" />

String button = request.getParameter("action");

If it's not in a form, you'll need some JavaScript, but your question implies it is.

The solution here is to have a hidden variable called action and on click of the button you can update the hidden variable with the value of the clicked button, then submit the form.

Ex:

<form action="" method="post" name="myform">
    <input type="hidden" name="action" />

    ....

    <button class="btn" value="save"  id="action" onclick="submitForm(this)"></button>
    <button class="btn" value="update"  id="action" onclick="submitForm(this)"></button>
    <button class="btn" value="query"  id="action" onclick="submitForm(this)"></button>
</form>

Then

<script>

    function submitForm(btn){
        var frm = document.myform;

        frm.action.value = btn.value;
        frm.submit();
        return false;

    }

</script>

All you need to add to your button is the attribute type="submit" and the attribute name="parametername". Then button tags act like inputs of type submit with the advantage that the value can be different than the display text or whatever you put inside the button tags.

If you want to submit your form with ajax I would use now the jquery ajaxform plugin to handle this.

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