Search code examples
springspring-mvchttp-request-parameters

How to keep request parameters after redirect?


I'm trying to resolve a bug when I send a form with an empty input.

This is my methode:

@RequestMapping(value = "/modifier.html", method = RequestMethod.POST)
public String modifier(ModelMap map, @ModelAttribute("FormObject") FormObject formObject, BindingResult result, HttpServletRequest req) {

    formObject.setModif(true);
    String idParam = req.getParameter("idTypeOuverture");

    if (result.hasErrors()) {
        return "redirect:/gestion.html?section=Configuration&panel=4&ouvrir=modifier";
    } else {
        //Instructions
}

When there are errors (empty input) the controller redirects to this link to tell user to correct errors. The problem is when I check parameters here they look correct (id, name ...), but the id becomes null in the following method:

@Override
public ModelAndView dispatcher(HttpServletRequest request, HttpServletResponse response) throws RorException {

    Map<String, Object> myModel = (Map<String, Object>) request.getAttribute(EnumParam.R_MY_MODEL.getKey());

    Enumeration<?> keys = request.getParameterNames();
    while (keys.hasMoreElements()) {
        String paramName = (String) keys.nextElement();
        String value = request.getParameter(paramName);
        myModel.put(paramName, value);
    }

    GlobalSession globalSession = (GlobalSession) getApplicationContext().getBean(Utilities.GLOBALSESSION_BEAN_REF);
    myModel.put("module", globalSession.getModule().getKeyMessage());
    String section = request.getParameter("section");

    // This instruction returns null
    String idForm = request.getParameter("id");
    id = Integer.parseInt(idForm);
    // This instruction returns NumberFormatException
    ObjectForm of = getForm(id);
    // ...
}

Well, I don't know why parameter id changed after redericting? do you have any idea? I tried to redifine parameters in the first method but still got the same NFE.

Thank you in advance.

Thank you


Solution

  • The request parameter is only for one request. You make a redirect, it means that you make another new "request".

    You should add it to the redirect:

    return "redirect:/gestion.html?section=Configuration&panel=4&ouvrir=modifier&idTypeOuverture="+idParam;