[英]How to pass value of radio button which is clicked to Spring MVC controller?
[英]SPRING MVC : How to pass the radio button value which is clicked to controller?
我正在使用 Spring MVC controller。
是否可以在单选按钮的路径值中设置 object?
在我的 jsp 内的表格下方
<spring:url value="/update" var="update" htmlEscape="false"/>
<form:form action="${update}" method="post" modelAttribute="addressForm">
<c:forEach items="${addresses}" var="address">
<form:radiobutton path="address" value="${address}" label="${address.city}"/>
</c:forEach>
<input type="submit" value="Confirm"/>
</form:form>
还有我的 controller
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String chooseAddress(@Valid final AddressForm form, final BindingResult bindingResult,
final RedirectAttributes redirectAttributes){
if (bindingResult.hasErrors()){
System.out.println("Erros");
}else{
System.out.println("NO Erros");
}
return REDIRECT_URL;
}
这是我的地址表
public class AddressForm implements Serializable {
private static final long serialVersionUID = 3734278553292263688L;
@NotNull
AddressDTO address;
public AddressDTO getAddress() {
return address;
}
public void setAddress(AddressDTO address) {
this.address = address;
}
}
我想从我的 controller 中检索选定的 object 地址,但我有一个错误bindingResult.hasErrors()
return true 并出现以下错误和地址 null
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'addressForm' on field 'address': rejected
value []; codes
[typeMismatch.AddressForm.address,typeMismatch.address,typeMismatch.com.data.AddressDTO,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [AddressForm.address,address]; arguments []; default message [address]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.data.AddressDTO' for property 'address'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.data.AddressDTO' for property 'address': no matching editors or conversion strategy found]
我建议你像这样在路径中传递地址 ID:
<spring:url value="/update" var="update" htmlEscape="false"/>
<form:form action="${update}" method="post" modelAttribute="addressForm">
<c:forEach items="${addresses}" var="address">
<form:radiobutton path="id"label="${address.city}"/>
</c:forEach>
<input type="submit" value="Confirm"/>
</form:form>
public class AddressForm implements Serializable {
private static final long serialVersionUID = 3734278553292263688L;
@NotNull
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
并且您在操作中通过 id 或代码恢复地址,因为您已经有了列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.