Sample.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:event listener="#{sample.dosamplelist}" type="preRenderView" />
</h:head>
<h:body>
<h:form>
<h:panelGrid id="samplesetting" columns="6" cellpadding="5">
<f:facet name="header">Name Setting</f:facet>
<h:outputLabel for="samplename" value="Name:" />
<p:inputText value="#{sample.name}" id="samplename"
required="true" label="samplename" />
</h:panelGrid>
<p:panel id="sampleview" header="Sample List">
<p:dataTable var="spl" value="#{sample.samplelist}" rowKey="#{spl.name}"
selection="#{sample.selectedname}"
selectionMode="single">
<p:column headerText="Name">
<h:outputText value="#{spl.name}" />
</p:column>
<p:column>
<p:commandButton id="one" value="View Details" action="#{sample.setSelectedsample(spl)}" update="@form:samplesetting">
</p:commandButton>
</p:column>
</p:dataTable>
</p:panel>
</h:form>
托管Bean
@SuppressWarnings("serial")
@ManagedBean(name = "sample")
@RequestScoped
public class Sample implements Serializable
{
private String name;
private List<Sample> samplelist;
private String selectedname;
//getters and setters
public void dosamplelist(ComponentSystemEvent event)
{
List<Sample> samplelist = new ArrayList<Sample>();
Sample configA = new Sample();
configA.setName("John");
samplelist.add(configA);
Sample configB = new Sample();
configB.setName("David");
samplelist.add(configB);
this.samplelist = samplelist;
}
public void setSelectedsample(Sample smpl)
{
this.name = smpl.name;
}
}
这是小-大表单的示例,需要的是,当我们从底部选择表行时,它将显示在顶部输入框中以进行编辑。
但当我按下命令按钮时,它不起作用。为什么?请问原因是什么?
可能的问题
一个明显的问题是,在类级别,您定义了:
private List<Sample> samplelist;
然后继续使用将变量隐藏在doSampleList
中
List<Sample> samplelist = new ArrayList<Sample>();
再加上您将bean标记为@RequestScoped
,这将保证在JSF请求处理过程中samplelist
的内容不会一致。
要解决:
将您的bean标记为@ViewScoped
,并根据需要解决变量隐藏问题。
进一步阅读:
- commandButton/commandLink/ajax-action/listaner方法未调用或输入值未更新