jsp中的<form:input>标签不转换为模型类的map属性的html输入标签

时间:2015-06-06 05:54:10

标签: java spring jsp spring-mvc

我在Category模型类中有CategoryAttribute对象的Map。由于某些原因,categoryAttributes map无法在jsp中与Spring Mvc的<form:input>标签绑定,但是jsp页面可以使用类别对象。我需要捕获类别属性属性名称和值的输入到控制器并持久到数据库我将如何做到这一点。我试过但<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 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
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>

这是尝试绑定类别类的地图对象时输入字段未来的页面

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您需要为所有成员变量生成getter和Setter。

第二:在表单中:输入应仅绑定到成员变量。所以请更改

    <form:input path="category.categoryAttributes['value']" cssClass="form-ontrol" />

    <form:input path="category.categoryAttributes['Key of HashMap'].memberVariableInCategoryAttribute" cssClass="form-ontrol" />