我的 jsp 页面上有这样的表单:
<form:select class="form-control" path="mark" >
<c:forEach begin="1" end="10" var="i" >
<option value="${i}">${i}</option>
</c:forEach>
</form:select>
我想将选定的值绑定(bind)到对象中的字段:
@Entity
public class Feedback {
private Integer mark;
// getters, setters...
}
现在,我有异常类型不匹配:无法将“字符串”转换为“整数”。
如何从
最佳答案
您遇到了TypeMismatch 异常,因为您试图将String
(请求参数具有String 值)绑定(bind)到Integer
。
你只需要解析它的值得到一个 int,使用 Integer.parseInt()
:
Feedback feedback = new Feedback();
if(request.getParameter("radios") != null) {
feedback.setMark(Integer.parseInt(request.getParameter("radios")));
}
并将name="radios"
添加到您的select
:
<form:select class="form-control" name="radios" path="mark" >
<c:forEach begin="1" end="10" var="i" >
<option value="${i}">${i}</option>
</c:forEach>
</form:select>
关于java - 我可以将 <option> 中的值作为 JSP 中的整数绑定(bind)到对象字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878285/