数字数据在Spring MVC中未绑定

时间:2019-05-01 11:16:19

标签: java spring spring-mvc jsp data-binding

在基于Spring MVC的项目中工作时,我试图将JSP中的数据绑定到模型。

由于某种原因,字符串数据完全绑定后,数字数据根本没有绑定。

我检查了POJO和JSP中的参数名称是否相同

下面是我的控制器代码

@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";
    }

下面是我的JSP代码

<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>

下面是我的模型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;
    }
}

由于某些原因,字符串数据完全绑定后,数字数据根本没有绑定

我检查了POJO和JSP中的参数名称是否相同

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

@ abhi314,您是否尝试过单独提取字段? 我的意思是您是否尝试过其中任何一种只是为了检查值是否来自视图侧?

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 
}

请通过这种方式让我知道您是否获得了价值

答案 1 :(得分:1)

您需要使用包装器Integer而不是int

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

我也建议使用命名约定标准。代替ID声明ID或最优选作为InvestorRegisterId。

int更改为Integer后,请重新生成相同的getter / setter。

您的实际问题在Java自动装箱/拆箱的概念上失败了。