java - 如何将双精度值从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;
}
}
解决方案
您对其中的对象和变量使用相同的名称。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 页面的控制器方法。
推荐阅读
- java - 我的 editor_layout_problem - 2 如何在 scoll 或 listview 中多次显示不同的片段
- java - Android RecyclerView 仅在关闭并重新启动应用后显示项目
- angular - 如何在 Spring MVC 中为 Angular 前端生成下载文件弹出窗口?
- r - 带假人的非线性最小二乘法
- mysql - MySQL Multiple INNER JOIN + GROUP BY 未按预期工作
- javascript - 如何更改 vuetify 数据表中单个按钮的属性?
- android - 有什么方法可以捕捉电话接听事件吗?
- javascript - 是什么导致我的代码中出现“RangeError:超出最大调用堆栈大小”?
- vue.js - Nuxt.js:管理静态生成网站的 CMS 集合
- java - JDBC 自定义类型映射顺序问题