ankesh mishra
ankesh mishra

Reputation: 35

checkbox not passing parameter when unchecked in spring mvc

I am using checkbox in my application to pass one value in spring .When i checked it it is working fine but the problem lying when I unchecked it.It shows error -

WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Required String parameter 'isFl' is not present

here isFl is the name of checkbox which I need to pass.I search lot and got many answer applied in my program but haven't got desired result.

If anyone knowing the answer please help thanks in advance.

controller program

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ModelAndView uploadPost(@RequestParam String fileName, @Validated Template template,
                               @Validated TemplateBean templateBean,
                              @RequestParam MultipartFile file, 
      HttpServletRequest request,**@RequestParam String isFl**) throws Exception
      {
        ModelAndView model = new ModelAndView("upload");


        if(isFl!=null){
            addTemplate.setFl(Integer.parseInt(isFl));
        }
        else{
            addTemplate.setFl(0);//false value
        }

jsp file

 <input id="checkbox" type="checkbox" **name="isFl"** value="1" >Facility letter

there is lot more things but i have only uploaded relevent to my question

Upvotes: 0

Views: 3168

Answers (4)

eastmael
eastmael

Reputation: 421

Sharing my answer related to checkboxes value not being sent when it's unchecked and its value is bound to a non-boolean field (i.e., String) since this question shows as a top result.

In Spring's WebDataBinder, it has a field marker described as DEFAULT_FIELD_DEFAULT_PREFIX. This is described in Spring's WebDataBinder API.

Basically, what it does is that it sends a default value when a certain checkbox is unchecked. This is particularly useful when a checkbox is bound to a non-boolean field (i.e., String)

The default field default prefix in Spring is an exclamation mark (!).

<input id="checkbox" type="checkbox" name="isFl" value="1" >Facility letter
<input type="hidden" name="!isFl" value="0" />

The codes above will send a value of "1" if the checkbox is ticked (or checked). Otherwise, it will send "0" (value of hidden input item).

P.S.

Upvotes: 0

Dr. Mehmet Ali ATICI
Dr. Mehmet Ali ATICI

Reputation: 609

Add a hidden input with prefix '_' added to the same name as below:

<input type="hidden" name="_isFl" value="on" >

Upvotes: 1

Nitin Gakhar
Nitin Gakhar

Reputation: 56

Use required = false @RequestParam(value = "isFl", required = false) String isFl

Upvotes: 4

Ismail Sahin
Ismail Sahin

Reputation: 2710

change your code like that ...HttpServletRequest request,@RequestParam Boolean isFl) because checkbox is a boolean value.

Required and String parameter 'isFl' is not present

But the parameter Boolean isFl is present in your form

Upvotes: 0

Related Questions