简体   繁体   中英

Number data is not binding in Spring MVC

While working on a spring MVC based project I am trying to bind data from JSP to model.

While the string data is binding perfectly for some reason the number data is not binding at all.

I have checked the parameter name it's same in POJO and JSP

Below is my controller code

@RequestMapping(value = "/investor-signup", method = RequestMethod.GET)
    public String registration(Model model) {
        model.addAttribute("investor", new InvestorRegister());
        return "investor-signup";
    }

    @RequestMapping(value = "/investor-signup", method = RequestMethod.POST)
    public String registration(@ModelAttribute("investor") InvestorRegister investor, BindingResult bindingResult, Model model) {

        System.out.println(investor.getFULL_NAME());
        System.out.println(investor.getMOB_NO());

        investorRegisterService.save(investor);
        return "redirect:/login";
    }

Below is my JSP code

<form:form method="post" action="investor-signup"
        modelAttribute="investor" id="contact-form"
        style="padding-top: 40px;" role="form" class="formcss" align="left">    

        <div class="controls">    
            <div class="row">    
                <div class="col-md-12">
                    <div class="form-group">    
                        <div class="col-md-2 ">
                            <label for="user_name">Full Name *</label>
                        </div>
                        <div class="col-md-10 p0">
                            <input id="user_name" type="text" name="FULL_NAME" class="form-control" required="required" data-error="Full Name is required.">
                        </div>
                        <div class="help-block with-errors"></div>
                    </div>
                </div>

                <div class="col-md-12">
                    <div class="form-group">
                        <div class="col-md-2">
                            <label for="user_mobile">Mobile *</label>
                        </div>    
                        <div class="col-md-6 ">
                            <input id="user_mobile" type="number" name="MOB_NO" class="form-control" required="required" data-error="Mobile No is required.">
                        </div>
                        <div class="help-block with-errors"></div>
                    </div>
                </div>              

            <div class="col-md-12" align="center">
                <input type="submit" class="btn btn-success btn-send" value="Register">
            </div>    

        </div>    
    </form:form>

Below is my model POJO

@Entity
@Table(name = "InvestorRegister")
public class InvestorRegister {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "IID_SEQ")
    @Column(name = "ID")
    private int ID;

    @NotEmpty
    @Column(name = "FULL_NAME")
    private String FULL_NAME;

    @Column(name = "MOB_NO")
    private int MOB_NO;

    public int getID() {
        return ID;
    }    
    public void setID(int iD) {
        ID = iD;
    }

    public String getFULL_NAME() {
        return FULL_NAME;
    }    
    public void setFULL_NAME(String fULL_NAME) {
        FULL_NAME = fULL_NAME;
    }

    public int getMOB_NO() {
        return MOB_NO;
    }    
    public void setMOB_NO(int mOB_NO) {
        MOB_NO = mOB_NO;
    }
}

While the string data is binding perfectly for some reason the number data is not binding at all

I have checked the parameter name it's same in POJO and JSP

Any help is appreciated.

@abhi314, Have you just tried by extracting field separately ? I mean have you tried any of these just to check value comes from view side or not?

public String registration(@RequestParam("FULL_NAME") String FULL_NAME, @RequestParam("MOB_NO") int MOB_NO) {
    //check value comes or not 
}

OR

public String registration(@RequestBody InvestorRegister  ir) {
    //check value comes or not 
}

Please do let me know if you get value or not by checking this way

You need to use Wrapper Integer instead of int .

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "IID_SEQ")
    @Column(name = "ID")
    private Integer ID;

Also I would suggest to use naming convention standards. Instead of ID declare id or most preferred as investorRegisterId.

After changing from int to Integer please regenerate the getter/setters for the same.

Your actual issue is failing on the concept of Java auto-boxing/unboxing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM