<Form: input> tag in jsp does not convert to html input tag for map property of model class
I have a CategoryAttribute object in a category model class. For some reason the categoryAttributes map cannot bind to the <form:input>
Spring Mvc tag in the jsp, however the category object is available to the jsp page. I need to catch the input of the name and value of the properties of the category attributes for the controller and save the database as I would do it. I tried but the tag is <form:input>
not converting to the input field, please see where I made the mistake, thanks for the help.
Category Model class
@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 class
@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
@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 page where I am trying to map object of the category class 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>
This is the page where the input field does not appear when trying to bind a category class map object
source to share
First you need to generate getters and setters for all member variables.
Second: in the form: the input must only bind to a member variable. So please change
<form:input path="category.categoryAttributes['value']" cssClass="form-ontrol" />
to
<form:input path="category.categoryAttributes['Key of HashMap'].memberVariableInCategoryAttribute" cssClass="form-ontrol" />
source to share