Java Server Faces - 如何更新网页上显示的List的值

时间:2015-08-12 07:00:56

标签: jsf primefaces

我正在为请求名称和值执行动态文本框但是当我单击我的.xhtml文件上的添加按钮时,它总是大小为1.我想获得List的当前大小然后添加另一个空白对象。

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);
}

}

这是我的.xhtml文件,它从bean

迭代List的值

的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>

1 个答案:

答案 0 :(得分:0)

我觉得你的大小总是1,因为在addRequestParameter方法中你只添加了一个元素。单击下一次此按钮,您将创建新请求并创建新的空列表。您必须将旧对象存储在某处(例如数据库)。