从Spring MVC中的下拉列表中接收Id而不是枚举文本值

时间:2019-03-22 13:02:51

标签: java spring jsp enums

我有一个枚举

package com.javarnd.bbms.enums;

public enum BloodTypeEnum {
    A_PLUS(1, "A+"), B_PLUS(2, "B+"), O_PLUS(3, "O+"), AB_PLUS(4, "AB+"), A_MINUS(5, "A-"), B_MINUS(6, "B-"),
    O_MINUS(7, "O-"), AB_MINUS(8, "AB-");

    private final int value;
    private final String name;

    private BloodTypeEnum(int value, String name) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }

    public static BloodTypeEnum getEnumByName(String name) {
        BloodTypeEnum[] modes = BloodTypeEnum.values();
        if (modes == null) {
            return null;
        }
        for (BloodTypeEnum mode : modes) {
            if (mode.getName().equals(name)) {
                return mode;
            }
        }
        return null;
    }

    public static BloodTypeEnum getEnumByValue(int value) {
        BloodTypeEnum[] modes = BloodTypeEnum.values();
        if (modes == null) {
            return null;
        }
        for (BloodTypeEnum mode : modes) {
            if (mode.getValue()==value) {
                return mode;
            }
        }
        return null;
    }
}

在我的控制器

@GetMapping("/add_donor_details")
    public ModelAndView addDonorDetails(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("admin/add_donor_details");
        DonorDetail donorDetail = new DonorDetail();
        mav.addObject("command", donorDetail);
        mav.addObject("pageTitle", "Add Donor Details");
        mav.addObject("submitBtn", "Add Donor");
        mav.addObject("bloodGroup", BloodTypeEnum.values());
        mav.addObject("action", "./save");
        return mav;
    }

    @PostMapping("/save")
    public ModelAndView saveDonorDetails(@ModelAttribute("command") DonorDetail donorDetail, HttpServletRequest request,
            final RedirectAttributes redirectAttributes) {
        ModelAndView mav = new ModelAndView("");
        DonorDetail dDetail = new DonorDetail();
        try {
            String bloodType = BloodTypeEnum.getEnumByValue(Integer.parseInt(donorDetail.getBloodGroup())).getName();
            dDetail.setBloodGroup(bloodType);
            dDetail.setDonorName(donorDetail.getDonorName());
            dDetail.setGuardianName(donorDetail.getGuardianName());
            dDetail.setAge(donorDetail.getAge());
            dDetail.setEmail(donorDetail.getEmail());
            dDetail.setContact(donorDetail.getContact());
            donorDetailService.save(dDetail);
            redirectAttributes.addFlashAttribute("successMsg", "Donor Details Saved Successfully");
            mav.setViewName("redirect:./add_donor_details");
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("errorMsg", "Failed to add Donor details,Try Again!!");
            mav.setViewName("redirect:./add_donor_details");
        }

查看行

        String bloodType = BloodTypeEnum.getEnumByValue(Integer.parseInt(donorDetail.getBloodGroup())).getName();

我需要执行此附加步骤,因为我没有像1,2,3,4那样获得枚举ID,而是获得了文本值。

下拉菜单正确显示在jsp中,但是当从JSP传递到控制器的值是Id形式而不是文本值时,我做错了什么?

JSP 代码段

<div class="form-group col-md-6">
                                <label class="control-label"><strong>Blood Group</strong><span style="color: #FF0000; font-size: 20px;">*</span></label>
                                <f:select path="bloodGroup" id="bloodGroup" class="form-control"
                                    data-validate="required" style=" width: 100%">
                                    <f:option value="">Select Blood Group</f:option>
                                    <c:forEach items="${bloodGroup}" var="bg">
                                        <f:option value="${bg.value}">${bg.name}</f:option>
                                    </c:forEach>
                                </f:select>
                            </div>

1 个答案:

答案 0 :(得分:0)

在DonorDetail类中,将bloodType的类型设置为int和

String bloodType = BloodTypeEnum.getEnumByValue(donorDetail.getBloodGroup()).getName();