0

I am doing dynamic textbox for request name and the value but when I click the add button on my .xhtml file it is always on size of 1. I want to get the current size of the List then add another blank object.

HomepageBean.java

@ManagedBean
public class HomepageBean {
private String url;
private String portNumber;
private List<RequestParameter> requestParameterList = new ArrayList<>();

public List<RequestParameter> getRequestParameterList() {
    return requestParameterList;
}

public void setRequestParameterList(List<RequestParameter> requestParameterList) {
    this.requestParameterList = requestParameterList;
}
/**
 * @return the url
 */
public String getUrl() {
    return url;
}

/**
 * @param url the url to set
 */
public void setUrl(String url) {
    this.url = url;
}

/**
 * @return the portNumber
 */
public String getPortNumber() {
    return portNumber;
}

/**
 * @param portNumber the portNumber to set
 */
public void setPortNumber(String portNumber) {
    this.portNumber = portNumber;
}

public void addRequestParameter(List<RequestParameter> requestParameters){
    System.out.println("size: " + requestParameterList.size());
    RequestParameter requestParameter = new RequestParameter();
    requestParameters.add(requestParameter);
    System.out.println("size: " + requestParameterList.size());
    this.setRequestParameterList(requestParameters);
}

public void removeRequestParameter(RequestParameter requestParameter){
    requestParameterList.remove(requestParameter);
}

}

and this is my .xhtml file which iterates the value of List from the bean

index.xhtml

 <?xml version='1.0' encoding='UTF-8' ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">>

<h:head>
    <h:outputStylesheet library="css" name="/WEB-INF/homepageStyle.css"/>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
    <h:outputLabel style="font-weight: bold" value="Platform V2 Emulator"/>
    <br />
    <h:panelGrid styleClass="panelGridCenter" columns="4" cellpadding="5">
    <h:outputLabel for="url" value="Url:" style="font-weight:bold" />
    <p:inputText id="url" value="#{homepageBean.url}" />
    <p:commandButton value="Submit" update="urlString" icon="ui-icon-check" />
    <h:outputText id="urlString" value="#{homepageBean.url}" />
    </h:panelGrid>
    <h:panelGrid styleClass="panelGridCenter" columns="4" cellpadding="5">
        <h:outputLabel for="portNumber" style="font-weight: bold" value="Port:"/>
        <p:inputText id="portNumber" value="#{homepageBean.portNumber}"/>
        <p:commandButton value="Submit" update="portNumberString" icon="ui-icon-check" />
        <h:outputText id="portNumberString" value="#{homepageBean.portNumber}" />
    </h:panelGrid>
    <h:panelGrid columns="6" cellpadding="5">
    <h:outputLabel for="parameters" style="font-weight: bold" value="Request Parameters:"/>
    </h:panelGrid>
    <h:panelGrid id="parametersGrid" columns="3" cellpadding="5">
        <c:forEach items="#{homepageBean.requestParameterList}" var="parameter">
            <h:inputText value="#{parameter.key}"/>
            <h:inputText value="#{parameter.value}"/>
            <h:commandButton action="#{homepageBean.removeRequestParameter(parameter)}" value="Remove"/>
        </c:forEach>
    </h:panelGrid>
    <h:panelGrid columns="1" cellpadding="5">
        <p:commandButton actionListener="#{homepageBean.addRequestParameter(homepageBean.requestParameterList)}" update="parametersGrid" value="Add"/>
    </h:panelGrid>
    <h:link outcome="welcomePrimefaces" value="Primefaces welcome page" />
    </h:form>
</h:body>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pabz Caroche
  • 35
  • 1
  • 8

1 Answers1

0

Well I think your size is always 1 because in your addRequestParameter method you add only one element. Clicking next time this button you create new request and new empty list is created. You have to store your older objects somewhere (e.g. database).

wawek
  • 1,577
  • 1
  • 13
  • 23
  • Hmmm what I want to do is get the current size of the List then add another object, and it will update the UI as a new pair of textbox will be added. – Pabz Caroche Aug 12 '15 at 07:11
  • Yes but while you click the button you creates new empty list every time. You can always use session bean if you are using EJB. They have to be stored between new requests. – wawek Aug 12 '15 at 07:14
  • Oh, you got the thing that I need I used annotation `@SessionScoped` then it worked. – Pabz Caroche Aug 12 '15 at 07:17