in Education by
I have an Enum 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; } } In My Controller @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"); } see the line String bloodType = BloodTypeEnum.getEnumByValue(Integer.parseInt(donorDetail.getBloodGroup())).getName(); I need to perform this additional step because, instead of getting text value I am getting the Enum Id like 1,2,3,4.. the drop down is properly renders in jsp, but when the value passed form the JSP to controller, is in the form of Id not in textual value, What I did wrong? JSP Snippet
Select Blood Group ${bg.name}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In the DonorDetail class set the type of bloodType to int and String bloodType = BloodTypeEnum.getEnumByValue(donorDetail.getBloodGroup()).getName();

Related questions

0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm using Semantic-UI-React in my React/Rails project and trying to use a Form.Select drop down ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I want to put a value from database because my dropdown already have a value the same as the text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have a jsp with two drop down location and department. The value of department dropdown is populated ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm trying to use MappingJacksonJsonView with Spring 3.0, without success. I don't know what I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I am having the following problem: I have an Enum that was originally declared with 5 elements. public enum ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Greetings! I have a DropDownList within a FormView which are bound to XmlDataSources: In the page's ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Greetings! I have a DropDownList within a FormView which are bound to XmlDataSources: In the page's ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I'm having a small issue with setting the initial value of a dropdown. The code below is the view ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    This is a Dropdown control where I am binding the data, after bind I am putting the select statement. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a text file, by which I am trying to write to in my program. Whenever user wants to add ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    first of all,i am fairly new with spring mvc so .. how springmvc find the right class to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I was wondering if I can customize my url into controller to accept null values @RequestMapping( value = " ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    Is it necessary to keep Spring MVC in the classpath for developing RESTful web services?...
asked Jun 24, 2021 in Technology by JackTerrance
0 votes
    What is Spring MVC?...
asked Jun 14, 2021 in Technology by JackTerrance
...