Java jsp中的标记未转换为模型类的map属性的html输入标记
我在Category model类中有CategoryAttribute对象的映射。由于某些原因,CategoryAttribute映射无法与jsp中Spring Mvc的标记绑定,但是CategoryAttribute对象可用于jsp页面。我需要捕捉类别属性、属性名称和值输入到控制器并持久化到数据库,我将如何做到这一点。我已经试过了,但标签并没有转换成输入字段。请看一下我犯了什么错误。谢谢你们的帮助 类别模型类Java jsp中的标记未转换为模型类的map属性的html输入标记,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我在Category model类中有CategoryAttribute对象的映射。由于某些原因,CategoryAttribute映射无法与jsp中Spring Mvc的标记绑定,但是CategoryAttribute对象可用于jsp页面。我需要捕捉类别属性、属性名称和值输入到控制器并持久化到数据库,我将如何做到这一点。我已经试过了,但标签并没有转换成输入字段。请看一下我犯了什么错误。谢谢你们的帮助 类别模型类 @Entity @Inheritance(strategy = Inhe
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "CATEGORY")
public class Category implements Serializable {
@Id
@Column(name = "CATEGORY_ID")
protected Long id;
@Column(name = "NAME", nullable = false)
@Index(name = "CATEGORY_NAME_INDEX", columnNames = { "NAME" })
protected String name;
@OneToMany(mappedBy = "category", targetEntity = CategoryAttribute.class, cascade = { CascadeType.ALL }, orphanRemoval = true)
@MapKey(name = "name")
@BatchSize(size = 50)
protected Map<String, CategoryAttribute> categoryAttributes = new HashMap<String, CategoryAttribute>();
}
控制器
@Controller
public class CategoryController {
private static final Logger logger = Logger
.getLogger(CategoryController.class);
@Autowired
private CatalogItemService catalogItemService;
public CatalogItemService getCatalogItemService() {
return catalogItemService;
}
public void setCatalogItemService(CatalogItemService catalogItemService) {
this.catalogItemService = catalogItemService;
}
@RequestMapping(value = "/redirectToForm", method = RequestMethod.GET)
public String retrieveForm(@ModelAttribute Category category) {
return "category";
}
//this function is responsible for sending the category object
@RequestMapping(value = "/gencategory", method = RequestMethod.GET)
public String genCategoryList(Model model, @RequestParam("id") String id) {
Category category = catalogItemService.findCategoryById(Long
.parseLong(id));
List<Category> categories = catalogItemService.findAllCategories();
List<CategoryMapper> childCategories = category
.getAllChildCategoryMappers();
List<CategoryMapper> parentCategories = category.getAllParentCategoryMappers();
model.addAttribute("categoryList", categories);
model.addAttribute("childCategoryList", childCategories);
model.addAttribute("parentCategoryList", parentCategories);
List<String> inventoryList = new ArrayList<String>();
inventoryList.add("ALWAYS_AVAILABLE");
inventoryList.add("UNAVAILABLE");
inventoryList.add("CHECK QUANTITY");
List<String> fulfillmentList = new ArrayList<String>();
fulfillmentList.add("Digital");
fulfillmentList.add("Gift card");
fulfillmentList.add("Pickup");
fulfillmentList.add("Physical Pickup or Ship");
fulfillmentList.add("Physical Ship");
model.addAttribute("category", category);
model.addAttribute("inventorIs", inventoryList);
model.addAttribute("fulfillmentIs", fulfillmentList);
return "generalcategory";
}
@RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
public String saveCategory(@ModelAttribute("category") Category category,
BindingResult bindingResult,
@ModelAttribute("hiddenFormValue") String hiddenFormValue,
Model model) {
Category defaultParentCategory = catalogItemService
.findCategoryById(Long.parseLong(hiddenFormValue));
category.setDefaultParentCategory(defaultParentCategory);
List<Category> categories = catalogItemService.findAllCategories();
model.addAttribute("categoryList", categories);
category.setId(29965L);
catalogItemService.saveCategory(category);
return "generalcategory";
}
@InitBinder
public void customDateBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
binder.registerCustomEditor(Date.class, "activeStartDate",
new CustomDateEditor(dateFormat, false));
binder.registerCustomEditor(Date.class, "activeEndDate",
new CustomDateEditor(dateFormat, false));
}
}
generalcategory.jsp
<form:form action="saveCategory" method="post" id="categoryForm" modelAttribute="category">
<c:set var="myRequestModel" value="${category}" scope="request" />
<c:out value="${myRequestModel.categoryAttributes}"></c:out>
<jsp:include page="categoryattributemodal.jsp">
<jsp:param name="category" value="${myRequestModel}" />
</jsp:include>
</form:form>
CategoryAttribute.jsp页面,我试图在其中映射category类的map对象
<div class="modal fade" id="modalCategoryAttribute" tabindex="-1"
role="dialog" aria-labelledby="myModelCattLabel" aria-hidden="true">
<div class="modal-dialog" aria-hidden="true">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<label for="key">Key*:</label>
<div class='input-group date' id='name'>
<form:input path="category.categoryAttribute['name']" cssClass="form-control" />
<!-- <input type="text" class="form-control" /> -->
</div>
</div>
<div class="form-group">
<label for="key">Attribute Value*:</label>
<div class='input-group date' id='attributeValue'>
<form:input path="category.categoryAttributes['value']" cssClass="form-control" />
<!-- <input type="text" class="form-control" /> -->
</div>
</div>
</div>
<div class="modal-footer">
<span class="text-muted"><input type="button" id="addCategoryAttrButton"
class="btn btn-primary" value="Save" /></span>
</div>
</div>
</div>
</div>
这是尝试绑定category类的map对象时输入字段不出现的页面
<div class="modal fade" id="modalCategoryAttribute" tabindex="-1"
role="dialog" aria-labelledby="myModelCattLabel" aria-hidden="true">
<div class="modal-dialog" aria-hidden="true">
<div class="modal-content">
<div class="modal-body">
<div class="form-group">
<label for="key">Key*:</label>
<div class='input-group date' id='name'>
<form:input path="category.categoryAttribute['name']" cssClass="form-control" />
<!-- <input type="text" class="form-control" /> -->
</div>
</div>
<div class="form-group">
<label for="key">Attribute Value*:</label>
<div class='input-group date' id='attributeValue'>
<form:input path="category.categoryAttributes['value']" cssClass="form-control" />
<!-- <input type="text" class="form-control" /> -->
</div>
</div>
</div>
<div class="modal-footer">
<span class="text-muted"><input type="button" id="addCategoryAttrButton"
class="btn btn-primary" value="Save" /></span>
</div>
</div>
</div>
</div>
首先需要为所有成员变量生成getter和setter 第二:在表单中:输入应该只绑定到成员变量。所以请换衣服
<form:input path="category.categoryAttributes['value']" cssClass="form-ontrol" />
到
<form:input path="category.categoryAttributes['Key of HashMap'].memberVariableInCategoryAttribute" cssClass="form-ontrol" />