我正在使用Spring Roo创建一个用于生成文档的应用程序。首先,用户创建一个文档:
public class Document {
@NotNull
private String titleDocument;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisDateField")
private Set<DateField> dateFields = new HashSet<DateField>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisRadioButton")
private Set<RadioButtonField> radioButtonFields = new HashSet<RadioButtonField>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "relatedDocumentToThisStringField")
private Set<StringField> stringFields = new HashSet<StringField>();
}
有3种类型的字段,为简化起见,这是StringField(其他字段几乎相同):
public class StringField extends Field {
@NotNull
private String valueString;
@NotNull
private Boolean isEditable;
@ManyToOne
private Document relatedDocumentToThisStringField;
@NotNull
private String nameStringField;
}
创建包含字段的文档时,另一个用户必须填写它。由于我不知道文档将有多少字段,我需要创建一个HashMap表单(I am following this tutorial)
我的HashMap是:
public class DynamicForm {
private Map<String, String> dynamicMap=new HashMap<String, String>();
public Map<String, String> getDynamicMap() {
return dynamicMap;
}
public void setDynamicMap(Map<String, String> dynamicMap) {
this.dynamicMap = dynamicMap;
}
}
在我的FillDocumentController中,我有:
@RequestMapping(value = "/{id}", params = "form", produces = "text/html")
public String updateForm(@PathVariable("id") Long id, Model uiModel) {
...
// Create DynamicForm Map
DynamicForm dynamicForm = new DynamicForm();
for (Field field : allFields) {
// Is StringField?
if (field.getClass().equals(StringField.class) == true) {
StringField stringField = (StringField) field;
if( stringField.getIsEditable() == true ) {
dynamicForm.getDynamicMap().put(stringField.getNameStringField(), stringField.getValueString() );
}
}
}
uiModel.addAttribute("dynamicForm", dynamicForm);
return "filldocuments/update";
}
这是观点:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<form:form action="/DocumentGenerator/filldocuments/add" modelAttribute="dynamicForm" method="post">
<c:forEach items="${dynamicForm.dynamicMap}" var="element">
<input name="element['${element.key}']" value="${element.value}"/>
</c:forEach>
<input type="submit" value="Save" />
</form:form>
</div>
这是捕获modelAttribute:
的方法@RequestMapping(value="/add", method = RequestMethod.POST, produces = "text/html")
public String update(@ModelAttribute(value="dynamicForm") DynamicForm dynamicForm, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
for( String key : dynamicForm.getDynamicMap().keySet() ) {
System.out.println("key="+key);
}
return "redirect:/filldocuments";
}
我的问题是dynamicForm是空的。 uiModel中还有另一个DynamicForm,也是空的(我处于调试模式)。用户填写的数据在哪里?我不知道出了什么问题!!
答案 0 :(得分:2)
我的错,观点必须是这样的:
<c:forEach items="${dynamicForm.dynamicMap}" var="dynamicMap">
<input name="dynamicMap['${dynamicMap.key}']" value="${dynamicMap.value}"/>
</c:forEach>
所以错误是使用变量元素