重定向后如何保留请求参数?

Chi*_*ski 3 spring spring-mvc http-request-parameters

当我发送带有空输入的表单时,我正在尝试解决一个错误。

这是我的方法:

@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
}
Run Code Online (Sandbox Code Playgroud)

当出现错误(空输入)时,控制器重定向到此链接以告诉用户更正错误。问题是当我在这里检查参数时,它们看起来是正确的(id、name ...),但在以下方法中 id 变为 null:

@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);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

好吧,我不知道为什么参数id在重新划分后发生了变化?你有什么主意吗?我尝试在第一种方法中重新定义参数,但仍然得到相同的 NFE。

先感谢您。

谢谢

Ruc*_*ini 6

尽管上一个答案已被接受,但我添加此答案仅供您参考。

您也可以使用带有和不带有 FlashAttributes 的 RedirectAttributes 在发出重定向之前,post 方法应该将 RedirectAttributes 作为参数这些属性将作为请求参数传递查看我的代码示例,看看它是否有帮助。

方式一:

@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";
}
Run Code Online (Sandbox Code Playgroud)

方式 2:无论在 flash 属性中添加什么数据都将添加到会话中 直到重定向成功才会在会话中存在 重定向时,从会话中检索数据并将其添加到模型中以用于新请求。只有重定向成功后,数据才会被移除

@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";
}
Run Code Online (Sandbox Code Playgroud)