Spring mvc: bind hash map with same / different value depending on user input

I have a requirement when I am asked to charge a fee for a certain course. But the problem is that for few courses the same fee applies to all types of users, and for some courses a professional fee applies. For example, if the user is a teacher, then he pays less than if he were an industry professional.

There are predefined professions in the database.

Here is my current logic with code.

Depending on the switch, I switch the correct div and certain logic at the back end.

<input type="radio" name="professionWiseFees" value="true"> <input type="radio" name="professionWiseFees" value="false"> <div id="totalAmount" class="hidden"> <input class="feeAmountOnly" name="feeAmount" type="text" /> </div> <div id="professionWiseAmount" class="hidden"> <c:forEach items="${applicationScope.professionList}" var = "profession" > Course Fee For ${profession.profession} <input type="hidden" value="${profession.id}" name="profession" type="text"/> <input class="feeAmoun2t" name="profession" type="text" /> </c:forEach> <div> 

Now I am checking what type of fees is applicable and filling out the hash map accordingly. if prefessionWiseFees is not applied, then adding the same fees to all professions.

 Boolean isProfessionWiseFeesApplicable = Boolean.parseBoolean(reqParams.get("professionWiseFees")[0]); Map<Integer,Double> feeProfessionMap = new HashMap<>(); List<Profession> professions = (List<Profession>) servletContext.getAttribute("professionList"); if(isProfessionWiseFeesApplicable) { String[] propfessionWiseFees = reqParams.get("profession"); // [1(profession_id),1000(fees),2,2000,3,7000], hence i+2 i=profession_id, i+1=fees for(int i=0; i < propfessionWiseFees.length-1 ;i=i+2){ feeProfessionMap.put(Integer.valueOf(propfessionWiseFees[i]),Double.parseDouble(propfessionWiseFees[i+1])); } } else { double feeAmount = Double.parseDouble(reqParams.get("feeAmount")[0]); for(Profession profession: professions){ feeProfessionMap.put(Integer.valueOf(profession.getId()),feeAmount); } } courseBean.setProfessionWiseFees(feeProfessionMap); courseBean.setProfessionWiseFees(isProfessionWiseFeesApplicable); 

Model class:

 public class CourseBean { // few fields private Map<Integer, Double> professionWiseFees; // <profession_id ,fees> // all setters and getters } 

So how can I solve this problem elegantly? requestParam.get in code

+8
spring spring-mvc
source share
2 answers

If you think that

Using requestParam.get reduces the readability and usability of the code.

You can use Spring binding.

First define a FeeForm bean:

 public class FeeForm { boolean isProfessionWiseFees; ProfessionAmount[] professionAmounts; Double feeAmount; } 

Then define a ProfessionAmount bean:

 public class ProfessionAmount { int profession; double amount; } 

Then change the controller method as follows:

  @RequestMapping(value = { "/test.html" }, method = RequestMethod.POST) public String testOverflow(Map<String, Object> model, FeeForm form) { Map<Integer,Double> feeProfessionMap = new HashMap<>(); List<Profession> professions = (List<Profession>) servletContext.getAttribute("professionList"); if(form.isProfessionWiseFees()) { for(ProfessionAmount f : form.getProfessionAmounts()){ feeProfessionMap.put(f.getProfession(),f.getAmount()); } } else { for(Profession profession: professions){ feeProfessionMap.put(profession.getId(),form.getFeeAmount()); } } 

HTML:

  <input type="radio" name="professionWiseFees" value="true"> true <input type="radio" name="professionWiseFees" value="false"> false <div id="totalAmount" class="hidden"> <input class="feeAmountOnly" name="feeAmount" type="text" /> </div> <div id="professionWiseAmount" class="hidden"> <c:forEach items="${applicationScope.professionList}" var = "profession" varStatus="status" > Course Fee For ${profession.profession} <input type="hidden" value="${profession.id}" name="professionAmounts[${status.index}].profession" type="text"/> <input class="feeAmoun2t" value='0' name="professionAmounts[${status.index}].fee" type="text" /> </c:forEach> <div> 
+1
source share

You should go to the jsp hold flag in the bean model, which indicates whether receipts are applicable or not based on this playback in the design and disable inapplicable parameters.

or

use AJAx. you can use the switch and use them to replace ajax. if this is an invalid return error message.

0
source share

All Articles