0

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

4
  • You might want to include the relevant code here. Commented May 31, 2016 at 4:33
  • ok please wait i am uploading Commented May 31, 2016 at 4:37
  • That has nothing to do with spring but on how the checkbox works. When not checked there will be no value (check the HTML spec for this). Instead of using a plain tag use the spring checkbox tag and make it part of your template bean. Commented May 31, 2016 at 7:28
  • that also result in same thing i tried Commented May 31, 2016 at 7:44

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Please explain your answer don't just respond with bare-code.
1

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

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.