0

I am having requirement to add dynamic dropdown and text boxes using JSD 2.x. Below is the JSF code I have written:

Java code I need to create multiple BenificiaryBean but don't know how to retrieve parameter values from requesthashmap as JSF generates name dynamically:

Below is sample jsf code:

<ui:repeat var="beneficiaryBean" value="#{customerContactBean.listBeneficiaryBean}"><tr>
  <td><h:inputText id="fullName" value="#{beneficiaryBean.fullName}"></h:inputText></td>
  <td><h:selectOneMenu id="gender" value="#{beneficiaryBean.gender}"> <f:selectItem itemValue="MALE" itemLabel="Male" /><f:selectItem itemValue="FEMALE" itemLabel="Female" /></h:selectOneMenu></td>
  <td><h:inputText id="age" value="#{beneficiaryBean.age}"></h:inputText></td>
  <td><h:selectOneMenu id="profileId" value="#{beneficiaryBean.profileId}"> <f:selectItems value="#{beneficiaryBean.profileValues}"

public List<BenificiaryBean> addBeneficiary(){
              Map<String,String[]> requestParameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap();

              listBeneficiaryBean = new ArrayList<BenificiaryBean>();

              while(requestParameters.entrySet().iterator().hasNext())
              {
                     BenificiaryBean temp = new BenificiaryBean();
                     temp.setAge(?);
                     temp.setFullName(?);
                     temp.setGender(?);
                     temp.setProfileId(?);
                     listBeneficiaryBean.add(temp);
              }
              showRemoveButton = true;
              return listBeneficiaryBean;
       }

What should be values of ? be?

Werner
  • 14,324
  • 7
  • 55
  • 77
  • Why do you use requestParameterMap for retrieve values?This is not the way of retrieve values from view. You should use value binding. Read more http://www.vogella.com/tutorials/JavaServerFaces/article.html#jsf_valuebinding – giaffa86 Dec 12 '14 at 13:36
  • Because I don't want to have individual array of String or other primitive types instead I want to represent BenificiaryBean object array representing one row. i can't paste jsf code here : – Sanjeev Thakur Dec 12 '14 at 13:44
  • Could you explain better your concept? what do you mean with individual array of String and BenificiaryBean object array ? – giaffa86 Dec 12 '14 at 13:48
  • It's unreadable, put that code in formatted way in the original post. – giaffa86 Dec 12 '14 at 13:59
  • I am unable to post jsf code in main topic and it's lenght in comments is fixed.Just to give u a pic,basically I hv application where multiple BeneficiaryBean will contain fullName text field,gender dropdown,profiles dropdown.I am using to iterate over list and hving actionListener to add each time new BeneficiaryBean in listBeneficiaryBean. – Sanjeev Thakur Dec 12 '14 at 14:04
  • The weird thing and that you call the property using as prefix "Bean". Are you a JSF newbie? You should create a form where each input is bound with a property (you just call it newBeneficiary and create getters and setters and put it into `customerContacBean`) using valueExpression language. – giaffa86 Dec 12 '14 at 14:16
  • Basically I am looking for an answer to retrieve same name multiple fields vlaues in jsf as we have getParameterValues(String name) in jsp/servlet. I agree i m new bie but only thing is that customerContactBean is acting as a controller here.As you can see in main query java code of addBeneficiary(), i tried retrieving request parameters values from facescontext but unable to retrieve it by name as it's name is dynamically generated like "jd1:0:fullName: – Sanjeev Thakur Dec 12 '14 at 14:23
  • I don't like JSP approach into JSF pattern but however you'll have time to learn how powerful is JSF. Yes usually JSF input id prefix is the "concatenation" of each container id up to form id(each JSF input must be inside an `h:form` if you want binding value to bean property). If you doesn't put id for someone, then JSF automatically will do, such as jd1. That :0 means that element is inside an iterated structure such as `c:forEach` or some datalist, datatable. So if you want retrieve by id input values, you should put the entire string. – giaffa86 Dec 12 '14 at 15:58
  • If you don't want "concatenation", you can use `prependId` attribute on `h:form` as described here http://stackoverflow.com/questions/3972653/jsf-why-prependid-false-in-a-form – giaffa86 Dec 12 '14 at 16:08
  • Using prependId doesn't worked as JSF is generating hidden form with unique id. Can you give me any link where mapping between jsf and managed beans given such that user can add or delete same set of rows ? Each row contains same name multiple text box,dropdowns.Even if I will map fullName text box with String[] fullName,it's giving error on tag for first time,how to map jsf and managed beans ? – Sanjeev Thakur Dec 15 '14 at 11:55
  • IMHO you should take a look to Primefaces. It is a library that works over JSF layer and has a lot of really powerful components. This component suits your needs [Primefaces Datatable](http://www.primefaces.org/showcase/ui/data/datatable/basic.xhtml). If you don't want further complexity take a look to [JSF Datatable](http://www.mkyong.com/jsf2/jsf-2-datatable-example/) – giaffa86 Dec 15 '14 at 14:15
  • Using : Map requestParameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap(); and then retrieving request parameter values from map by passing keys made my job:Set keys = requestParameters.keySet();for(String key : keys) { if(key.contains("fullName")) { sizeOfBenf++; } } – Sanjeev Thakur Dec 16 '14 at 10:19
  • Using : Map requestParameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap(); and then retrieving request parameter values from map by passing keys made my job:Set keys = requestParameters.keySet();for(String key : keys){ if(key.contains("fullName")){sizeOfBenf++;} }for(int i=0;i – Sanjeev Thakur Dec 16 '14 at 10:30
  • I don't know what you try to say. In JSF you don't need to user requestParameterValuesMap, you can directly bind value from view into a bean property. – giaffa86 Dec 16 '14 at 11:13
  • Who will mark this query resolved with my ans ? – Sanjeev Thakur Dec 16 '14 at 11:51

0 Answers0