Pergunta

In Spring I usually did a redirect-after-submit(get method mapping) to prevent the user of submitting a form , but when i pressing F5 it will go to get method mapping again and display me this kind of confirm message. how could i prevent this message every time on F5.

enter image description here

Here is the code for controller -

 ScheduleDetail objScheduleDetail = new ScheduleDetail();       
 HttpSession session = request.getSession(true);
 String condition = "";
 try{
      int careProfessionalIDF = (Integer) session.getAttribute("careProfessionalIDF");
      condition = "CareProfessionalIDF = "+careProfessionalIDF;
      objScheduleDetail.setCareProfessionalIDF(careProfessionalIDF);

    }catch (Exception e) {
      int careProviderIDF = (Integer) session.getAttribute("careProviderIDF");
      condition = "CareProviderIDF = "+careProviderIDF;
      objScheduleDetail.setCareProviderIDF(careProviderIDF);
    }

    List<ScheduleDetail> ScheduleDetailList = objScheduleDetailManager.getAllScheduleDetail(condition+" ORDER BY ScheduleDetailIDP DESC");
    model.addObject("List_of_ScheduleDetail",ScheduleDetailList);
    model.addAttribute("ScheduleDetail", objScheduleDetail);
    return "hospital/scheduleDetail";//jsp page

edited code

 @RequestMapping("/editAddressType.html")
    public String editAddressType(ModelMap model,HttpServletRequest request)
    {
        int addressTypeIDP = Integer.parseInt(request.getParameter("AddressTypeIDP"));

        AddressType objAddressType = new AddressType();
        objAddressType = objAddressTypeManager.getByID(addressTypeIDP); 

        model.addAttribute("AddressType", objAddressType);

        return "jsp/addressType"; 

it open addressType.jsp with data tht we bind with `model.addAttribute`. now if i press F5 it show alert message as above image.

**get method**

    @RequestMapping(value="/getAddressType.html", method=RequestMethod.GET)
    public String getAddressType(ModelMap model, HttpServletRequest request) throws RemoteException
    {
        AddressType objAddressType = new AddressType();
        model.addAttribute("AddressType", objAddressType);

        return "hospital/addressType";
    }
Foi útil?

Solução

If you implement the POST - REDIRECT - GET pattern correctly, you will not see the warning from the browser that you mentioned. The warning is shown when the Page being viewed is in response to a POST request. The traditional pattern, FORM - POST - SUCCESS page.

The code you provided in the question is not enough to reach the root cause of the problem. I'm listing key points of the implementation here, please compare with your code and you'll understand what the mistake is.

  1. To show the user the form, where they are supposed to enter data for submission. (Just the starting point, could be any page in your application.)

    @RequestMapping(value = "/checkout/{cartId}.htm", method = RequestMethod.GET)
    public ModelAndView showCheckoutForm(...) {
         ......
         return new ModelAndView("/WEB-INF/jsp/form.jsp")
    }
    
  2. The form POSTs to a handler method, which issues a redirect to the user, pointing to a URL that will show the details of the resource created as a result of the POST.

    @RequestMapping(value = "/checkout/{cartId}.htm", method = RequestMethod.POST)
    public View submitCheckoutForm(....) {
         return new RedirectView("/checkout/confirmation/" + cartId + ".htm");
    }
    
  3. The details of the created resource will be shown by a handler method like the following. Note that at this point, if your implementation worked properly, the URL in the user's browser will change to the path redirected by the POST handler. And a fresh GET request will be issued to the server.

    @RequestMapping(value = "/checkout/confirmation/{cartId}.htm", method = RequestMethod.GET)
    public ModelAndView showCheckoutConfirmation(....) {
          return new ModelAndView("viewThatShowsCheckoutConfirmation");
    }
    

If your implementation is similar, the confirmation page is a result of a GET request, so browsers won't issue a warning for re-POSTing data if you refresh the page.

My suspicion is that your POST handler is not issuing a redirect to the user, it is instead rendering the confirmation page itself.

Hope this helps. Please let me know if this does not solve your problem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top