< form:input> jsp中的标记未转换为模型类的map属性的html输入标记 [英] <form:input> tag in jsp is not converting to input tag of html for map property of model class

查看:39
本文介绍了< form:input> jsp中的标记未转换为模型类的map属性的html输入标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Category模型类中具有CategoryAttribute对象的Map.由于某些原因,categoryAttributes映射无法与jsp中Spring Mvc的<form:input>标记绑定,但是category对象可用于jsp页面.我需要捕获类别属性属性名称和值的输入到控制器,并坚持到数据库,我该怎么做.我已经尝试过,但是<form:input>标记未转换为输入字段,请查看我做错了的地方,谢谢您的帮助.

I have Map of CategoryAttribute object in Category model class .Due to Some reason categoryAttributes map is not able bind with <form:input> tag of Spring Mvc in jsp, however category object is available to jsp page. I need to catch input of Category Attributes properties name and value to controller and persist to database how would I do that. I have tried but <form:input> tag is not converting into input field please have a look where i have done mistake thanks for helping.

类别模型类

 @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类

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

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页面,我试图在其中映射类别类的地图对象

CategoryAttribute.jsp page where i am trying to map the map object of category class

    <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 input field is not coming when try to bind map object of category class

推荐答案

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

First you need to generate getters and Setters to all member variables.

第二个:形式:输入应仅绑定到成员变量.因此,请更改

Second : In form:input should bind to member variable only. So please change

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

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

这篇关于&lt; form:input&gt; jsp中的标记未转换为模型类的map属性的html输入标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆