0

This is my controller code.

@RequestMapping(value = "/save", method = RequestMethod.POST)
public final String submit(@ModelAttribute("loanType") @Valid  LoanType loanType,   
BindingResult binding, RedirectAttributes attr, HttpSession session) {

    if (binding.hasErrors()) {
        attr.addFlashAttribute("org.springframework.validation.BindingResult.loanType",  
binding);
        attr.addFlashAttribute("loanTypeForm", binding);
        return "redirect:/loanType/addLoanType";
    }
       loanTypeService.insertLoanType(loanType);
       log.info("loan type saved succesfully with loan type id =" + 
loanType.getLoanTypeId() + " and name =" + loanType.getLoanTypeName());
       attr.addFlashAttribute("message", "Loan Type named= '" + 
loanType.getLoanTypeName() + "' is added Successfully");


    return "redirect:/loanType/listLoanType";
 }

This is my view page.

         <tr>
         <td>&nbsp;</td>
         <td><form:input cssErrorClass="errorinput" path="loanTypeName"                
               alt="LoanTypeName"     placeholder="Loan Type Name:" /></td>  
         <td><form:errors path="loanTypeName" cssClass="error"/> 
         </td>

         </tr>  

         <tr>
          <td>&nbsp;</td>
            <td><br>
            <table class="form-subtable">
            <tr>
             <td class="formtitle-subtitle">
                     Loan Details
                </td>
             </tr>
             <tr>
               <td><form:errors path="documents"  cssClass="error"/></td>
             </tr>
             <tr>
               <td><form:checkboxes cssClass="regular- checkbox" path="documents"  
                     items="${documentList}" itemLabel="documentName"  
                     itemValue="documentId" delimiter="<br>"/>                                                   
               </td>

           </tr>

         </table>
       </td>
       <td>&nbsp;</td>

         </tr>
         <tr>  
        <td></td>  
        <td>
          <br><div class="submit"><input  class="submit" type="submit" value="Save" />
                <input type="reset" value="Cancel" /></div>
                     </td>
                       <td></td>

                            </tr>
   </table> 

When I submit the form without filling the text field and only choosing the checkboxes, it shows the error messages but the previoulsy selected checkboxes gets dis-selected...I want them selected when the controller returns the error messages...

Can anyone help me?

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

1

Try

return "loanType/addLoanType";

instead of

return "redirect:/loanType/addLoanType";

OR add command/model as flash attribute

redirectAttributes.addFlashAttribute("loanType", loanType);

redirect is a complete new request and anything stored in existing request will be destroyed.

Read more here and here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

you can do it manually in your controller redirect to the previous page if there is an error and save request scope attribute that you want to return

@RequestMapping(value = "/save", method = RequestMethod.POST)
public final String submit(@ModelAttribute("loanType") @Valid  LoanType loanType, BindingResult binding, RedirectAttributes attr, HttpSession session,HttpServletRequest request) {

if (binding.hasErrors()) {

//set request scope attribute ex name
request.setAttribute("name","testName");
    return "redirect:/loanType/addLoanType";
}
   loanTypeService.insertLoanType(loanType);
   log.info("loan type saved succesfully with loan type id =" + loanType.getLoanTypeId()+ " and name =" + loanType.getLoanTypeName());
   attr.addFlashAttribute("message", "Loan Type named= '" + loanType.getLoanTypeName() + "' is added Successfully");


return "redirect:/loanType/listLoanType";
 }

and on your jps page retrieve the attribute with EL.

<form:input path="Name" alt="Name"     placeholder="Name:" value="${name}" />
Ker p pag
  • 1,568
  • 12
  • 25