What is the smartest way to handle "boolean" parameter passed by a form submission into a Spring MVC controller method?

11,118

1) Exist a way to specify (in the HTML) that if the checkbox is not check the value have to be false (avoiding to use JavaScript).

Add hidden input with same name and value as false:

<form action="/someaction" enctype="multipart/form-data">
    <input type="checkbox" name="isMaster"/>
    <input type="hidden" name="isMaster" value="false"/>
</form>

Note that hidden input have to be specified after checkbox.

2) Or can I handle this situation into the controller method signature?

Yes, you can. Use defaultValue property of @RequestParam annotation:

@RequestMapping(value = "/", method = RequestMethod.POST)
public String handleFileUpload(
    @RequestParam(name = "isMaster", defaultValue = "false") Boolean isMaster,
    @RequestParam MultipartFile[] fileUpload){

    ...
}

In this case, if checkbox will not be checked, value of isMaster will be false. And you don't need to add excess hidden input into view.

3) Can I pass a boolean value instead a string one into the HTML form?

Of course you can:

@RequestMapping(value = "/form", method = RequestMethod.GET)
public String showForm(ModelMap model){
    model.addAttribute("isMaster", true);
    return "someview";
}

Then in view you can use passed value for render checkbox next way:

<form action="/someaction" method="post" enctype="multipart/form-data">
    <input type="checkbox" name="isMaster" ${isMaster?'checked':''}/>
    ...
</form>
Share:
11,118
AndreaNobili
Author by

AndreaNobili

Updated on June 13, 2022

Comments

  • AndreaNobili
    AndreaNobili almost 2 years

    I am working on a Spring MVC application and I have the following doubt about what is the smarter way to handle a boolean parameter into a POST requests sended by a form submission.

    So I have the following simple html page that contains a form:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spring MVC - Hibernate File Upload to Database Demo</title>
    </head>
    <body>
        <div align="center">
            <h1>Spring MVC - Hibernate File Upload to Database Demo</h1>
            <form method="post" action="http://localhost:8080/AccomodationMedia/" enctype="multipart/form-data">
                <table border="0">
                    <tr>
                        <td>Pick file #1:</td>
                        <td><input type="file" name="fileUpload" size="50" /></td>
                    </tr>
                    <tr>
                        <td>Pick file #2:</td>
                        <td><input type="file" name="fileUpload" size="50" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Upload" /></td>
                    </tr>
    
                    <tr>
                        <input type="checkbox" name="isMaster" value="true">Is Master Image<br>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>
    

    So, as yu can se it contains an input field named isMaster that if checked send the String value true (if it is not checked this parameter value will be null).

    Then I have this controller class:

    @RestController
    @RequestMapping("/AccomodationMedia")
    public class AccomodationMediaController {
    
        .....................................................................
        .....................................................................
        .....................................................................
    
        @RequestMapping(value = "/", method = RequestMethod.POST)
        public String handleFileUpload(HttpServletRequest request,
                                       @RequestParam MultipartFile[] fileUpload) throws Exception {
                                       //@RequestParam String isMaster) throws Exception {
    
            System.out.println("handleFileUpload() START");
    
            boolean isMaster;
            String isMasterString = request.getParameter("isMaster");
    
            if(isMasterString == null) {
                isMaster = false;
            }
            else {
                isMaster = true;
            }
    
            if (fileUpload != null && fileUpload.length > 0) {
                for (MultipartFile currentFile : fileUpload){
    
                    System.out.println("Saving file: " + currentFile.getOriginalFilename());
    
                    accomodationMediaService.saveAccomodationMedia(currentFile, isMaster);
    
                }
            }
    
            return "Success";
        }
    
    }
    

    So there is the handleFileUpload() method that handle POST request toward the URI: http://localhost:8080/AccomodationMedia/

    To obtain a boolean value related to the isMaster parameter I have done:

    String isMasterString = request.getParameter("isMaster");
    

    in the method body (so I am retrieving it from the HttpServletRequest request parameter).

    I have also tryied to change the method signature in this way:

    public String handleFileUpload(HttpServletRequest request,
                                       @RequestParam MultipartFile[] fileUpload,
                                       @RequestParam String isMaster) throws Exception {
    

    It only works if the html form checkbox is checked, otherwise the isMaster parameter is null and it go into exception.

    So I have the following doubts:

    1) Exist a way to specify (in the HTML) that if the checkbox is not check the value have to be false (avoiding to use JavaScript).

    2) Or can I handle this situation into the controller method signature?

    3) Can I pass a boolean value instead a string one into the HTML form?