首页 > 解决方案 > 如何将双精度值从jsp存储到spring mvc控制器

问题描述

这是我的 JSP 页面:

    <c:url var="productSave" value="/admin/products/emi.html" />
    <form:form method="Post" action="${productSave}"
    modelAttribute="intrest">
    <table>
        <tr>
            <td>Id :</td>
            <td><form:input path="financerId" /></td>
        </tr>
        <tr>
            <td>downpayment :</td>
            <td><form:input path="downpayment" /></td>
        </tr>
        <tr>
            <td>months :</td>
            <td><form:input path="months" /></td>
        </tr>
        <tr>
            <td>intrest :</td>
            <td><form:input  path="intrest" /></td>
        </tr>
        <tr>
            <td></td>
            <td><input onclick="self.close()" type="submit" value="Save" /></td>
        </tr>
    </table>
    </form:form>    

在这里,我将intrest字段作为double模型 bean。但是当我尝试保存它抛出的实体时

Failed to convert value of type 'java.lang.String' to required type 'com.salesmanager.core.model.catalog.product.relationship.FinancerRateofIntrest'; nested exception is org.springframework.core.convert.ConversionFailedException

控制器 :

@RequestMapping(value = "/admin/products/emi.html", method = RequestMethod.POST)
public String GetEmiOption(@Valid @ModelAttribute("intrest") FinancerRateofIntrest intrest, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        rateofintrest.save(intrest);
        return "admin-products-emi";
}

实体类:

@Entity
@Table(name = "FinancerRateofIntrest", schema = SchemaConstant.SALESMANAGER_SCHEMA)
public class FinancerRateofIntrest extends SalesManagerEntity<Long, FinancerRateofIntrest> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID", unique = true, nullable = false)
    @TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "PRODUCT_RELATION_SEQ_NEXT_VAL")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
    private Long id;
    private int financerId;
    private String downpayment;
    private String months;
    private Double intrest;





    public Double getIntrest() {
        return intrest;
    }

    public void setIntrest(Double intrest) {
        this.intrest = intrest;
    }

    public Long getId() {

        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getFinancerId() {
        return financerId;
    }

    public void setFinancerId(int financerId) {
        this.financerId = financerId;
    }

    public String getDownpayment() {
        return downpayment;
    }

    public void setDownpayment(String downpayment) {
        this.downpayment = downpayment;
    }

    public String getMonths() {
        return months;
    }

    public void setMonths(String months) {
        this.months = months;
    }

}

标签: javaspringhibernatespring-mvc

解决方案


您对其中的对象和变量使用相同的名称。modelAttribute将from的变量名称更改intrest为其他名称,例如financerRateofIntrest

<form:form method="Post" action="${productSave}" modelAttribute="financerRateofIntrest"> 

@RequestMapping(value = "/admin/products/emi.html", method = RequestMethod.POST)
public String GetEmiOption(@Valid @ModelAttribute("financerRateofIntrest") FinancerRateofIntrest intrest, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        rateofintrest.save(intrest);
        return "admin-products-emi";
}

此外,从返回 jsp 页面的控制器方法。


推荐阅读