How can I access methods based on strings in an array?

**Hello!** I'm not even sure if this is possible, but hopefully it is in Java. I know I've done it in PHP by using variable variables and accessing a variable dynamically, but I read this isn't possible in Java in a different question. I have an array of strings which contains the state of checkboxes in my JSF GUI. If all checkboxes are set the array will then be filled with strings containing the value of different checkboxes. The labels and values of the checkboxes are built by looping through an array that looks like this: private static final String[][] OVERVIEW_FIELDS = {{"ID", "requestid"}, {"Date added", "dob"}, {"Title", "requestTitle"}, {"Process", "processId"}, ..., ..., ...}; Then I iterate through the array and make JSF SelectItems out of it: for (int i = 0; i < OVERVIEW_FIELDS.length; i++) { SelectItem item = new SelectItem(OVERVIEW_FIELDS[i][0], OVERVIEW_FIELDS[i][1]); getFields().add(item); } **The problem and current solution (hopefully someone can help me refactor it into something better:** for (String item : getVisibleFields()) { if (item.equals("dob")) { setDob(true); } if (item.equals("requestTitle")) { setRequestTitle(true); } if (item.equals("processId")) { setProcessId(true); } if (...) { } } And so on it continues for 11 booleans. I don't like this solution, and I see similar patterns in other beans in my system. Does anyone know if there's a better solution to this? The booleans control columns where I set their rendered field to true/false according to what the checkbox array contains. I was hoping in the JSF EL I could access the visibleFields variable and the contains method, thus returning either true or false if the object exist in array. This doesn't seem to work though and the EL doesn't give me access to the contains method. I'm currently using: rendered="#{overviewFilterBean.dob}" And I tried with this code: rendered="#{overviewFilterBean.visibleFields.contains['dob']}"> Now this led me to try to use my final array and set the booleans according to `getFields().contains(OVERVIEW_FIELDS[i][1])`, but how can I make this possible? Part real and part pseudo code follows (the solution I was aiming for now): for (int i = 0; i

以上就是How can I access methods based on strings in an array?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准