【发布时间】:2015-06-06 05:54:09
【问题描述】:
我在 Category 模型类中有 CategoryAttribute 对象的 Map。由于某种原因 categoryAttributes 映射无法与 jsp 中 Spring Mvc 的 <form:input> 标记绑定,但是类别对象可用于 jsp 页面。我需要将 Category Attributes 属性名称和值的输入捕获到控制器并保存到数据库中,我将如何做到这一点。我试过了,但是<form:input> 标签没有转换成输入字段,请看看我在哪里做错了谢谢你的帮助。
类别模型类
@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>();
}
CategoryAttribute 类
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "CATEGORY_ATTRIBUTE")
public class CategoryAttribute implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "CATEGORY_ATTRIBUTE_ID")
protected Long id;
@Column(name = "NAME", nullable = false)
@Index(name = "CATEGORYATTRIBUTE_NAME_INDEX", columnNames = { "NAME" })
protected String name;
@Column(name = "VALUE")
protected String value;
@ManyToOne(targetEntity = Category.class, optional = false)
@JoinColumn(name = "CATEGORY_ID")
@Index(name = "CATEGORYATTRIBUTE_INDEX", columnNames = { "CATEGORY_ID" })
protected Category category;
}
控制器
@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 页面,我试图在其中映射类别类的地图对象
<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>
这是尝试绑定类别类的地图对象时输入字段不来的页面
【问题讨论】:
标签: java spring jsp spring-mvc