我有条件要求我获取适用于某些课程的费用。但问题是,对于很少的课程,相同的费用适用于所有类型的用户,对于某些课程,适用专业费用。例如,如果用户是教师,那么他的费用与他是行业专业人员的费用不同。
数据库中有预定义的专业。
这是我当前的代码逻辑。
根据单选按钮,我在后端切换正确的div和特定逻辑。
<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>
现在我正在检查适用的费用类型并根据它填写hashmap。如果prefessionWiseFees不适用,则向所有专业添加相同的费用。
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);
模特课:
public class CourseBean {
// few fields
private Map<Integer, Double> professionWiseFees; // <profession_id ,fees>
// all setters and getters
}
那么如何优雅地解决这个问题我发现使用requestParam.get
会降低代码的可读性和可维护性。
答案 0 :(得分:1)
如果您认为
使用requestParam.get降低代码的可读性和可维护性。
您可以使用Spring网页绑定。
首先定义一个FeeForm
bean:
public class FeeForm {
boolean isProfessionWiseFees;
ProfessionAmount[] professionAmounts;
Double feeAmount;
}
然后定义ProfessionAmount
bean:
public class ProfessionAmount {
int profession;
double amount;
}
然后以这种方式更改控制器方法:
@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 :(得分:0)
您应该来到模型bean中的jsp持有标志,该标志指示费用是否适用,因为它在设计上播放并禁用不适用的选项。
或
使用AJAx。您可以使用单选按钮并使用它们的更改来通过ajax进行检查。如果它不适用返回验证错误消息。