我正在为请求名称和值做动态文本框,但当我单击.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,因为在你的addRequestParameter
方法你只添加一个元素。下次单击此按钮时,将创建新的请求和新的空列表。你必须将旧的对象存储在某个地方(例如数据库)。