springmvc 表单提交 controler 层需要接收 list 对象参数 ,但是昨天在家里弄怎么试都不成功,有人知道怎么搞吗?
页面代码
<form action="swingAdd.html" method="post">
<hr/>
<p>id:<input type="text" name="list[0].id"/>,
root: <select name="list[0].root"><option value="false">false<option><option value="true" selected="selected">true<option><select>,
expression:<input type="text" name="list[0].expression"/>,
validate_type:<select name="list[0].validateType"><option value="buyPrice">buyPriceo<ption><select>,
expect:<input type="text" name="list[0].expect"/>,
or_else:<input type="text" name="list[0].orElse"/>,
child_id:<input type="text" name="list[0].child"/>。 <input type="button" onclick="addChild(this);" value="增加子节点"/>
<p>
<input type="submit" value="提交"/>
<form>
controller 代码
@RequestMapping(value = "/swingAdd", method = RequestMethod.POST)
public String swingAdd(SwingFormList formList, Model model) {
// System.out.println(swingList);
return "swing/swingRule";
}
SwingFormList 源码
public class SwingFormList {
private List<Swing> list;
public List<Swing> getList() {
return list;
}
public void setList(List<Swing> list) {
this.list = list;
}
}
Swing 源码
public class Swing {
//REQUIRED
private String id;
private String expression;
private Validater validateType;
private String expect;
//IMPLIED
private boolean autoTrigger;
private String orElse;
private String executor;
private Swing child;
//other
private boolean root;
}
没拷贝 get/set 方法了
页面错误信息
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='swingFormList'. Error count: 1
补充出错原因
child name 为 child_id:<input type="text" name="list[0].child"/>
, 而 Swing.class.isAssignableFrom(org.eddy.swing.entity.Swing#child.getClass())
spring 会尝试获取参数为一个字符串类型的构造函数并 newInstance
org.springframework.beans.TypeConverterDelegate#convertIfNecessary(java.lang.String, java.lang.Object, java.lang.Object, java.lang.Class, org.springframework.core.convert.TypeDescriptor)
else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {
if (conversionAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {
try {
Constructor<T> strCtor = requiredType.getConstructor(String.class);
return BeanUtils.instantiateClass(strCtor, convertedValue);
}
catch (NoSuchMethodException ex) {
// proceed with field lookup
if (logger.isTraceEnabled()) {
logger.trace("No String constructor found on type [" + requiredType.getName() + "]", ex);
}
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Construction via String failed for type [" + requiredType.getName() + "]", ex);
}
}
}
由于我没有参数为一个 String 对象的构造函数,导致产生了异常。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于