How to keep request parameters after redirect?

10.7k views Asked by At

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

3

There are 3 answers

0
pL4Gu33 On BEST ANSWER

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;

0
Ruchi Saini On

Although the previous answer is accepted, I am adding this answer just for your information.

You can also use RedirectAttributes with and without FlashAttributes also Before issuing redirect, post method should take RedirectAttributes as argument These attributes will be passed as request parameters Look at my code example and see if its helpful.

Way 1 :

@RequestMapping(value={"/requestInfo.html"}, method=RequestMethod.POST)
public String requestInfoPost1(
    @ModelAttribute("requestInfoData") RequestInfoData requestInfoData,
    BindingResult result,
    RedirectAttributes redirectAttributes, 
    SessionStatus status
) { 
       // some logic
       redirectAttributes.addAttribute("name", requestInfoData.getName());
       redirectAttributes.addAttribute("age", requestInfoData.getAge());
       // some logic
       return "redirect:requestInfoSuccessRedirect";
}

@RequestMapping("requestInfoSuccessRedirect")
public String requestInfoSuccessRedirect()
{
    return "requestInfoSuccess";
}

Way 2: Whatever data is added in flash attribute will be added in session It will be in session only till redirect is successful On redirect, data is retrieved from session and added to Model for new Request. Only after redirect is successful, data is removed

@RequestMapping(value={"/requestInfo.htm"}, method=RequestMethod.POST)
public String requestInfoPost(
    @ModelAttribute("requestInfoData") RequestInfoData requestInfoData,
    BindingResult result, 
    RedirectAttributes redirectAttributes,
    SessionStatus status
) { 
    // some logic
    redirectAttributes.addFlashAttribute("requestInfoData",  
    requestInfoData);
    // some logic
    return "redirect:requestInfoSuccessRedirect";
}

@RequestMapping("requestInfoSuccessRedirect")
public String requestInfoSuccessRedirect()
{
    return "requestInfoSuccess";
}
0
Kevin Day On

Using RedirectAttributes is the correct way to do this as @Rucki Saini posted.

If you want to pass all attributes without having to list every one out individually, here's an elegant way to do that:

@GetMapping("/upload/**")
public String redirectSubUrlsToMain(RedirectAttributes redirectAttributes, HttpServletRequest req) {
    redirectAttributes.addAllAttributes(req.getParameterMap());
    return "redirect:/upload";
}