命令按钮不调用函数,不工作更新属性

时间:2014-03-31 00:11:38

标签: jsf

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

这是小形式的样本,需要的是,当我们从底部选择表格行时,它将显示在顶部输入框中进行编辑。

但是当我按下命令按钮时,它不起作用。为什么?请问是什么原因?

1 个答案:

答案 0 :(得分:0)

可能出现的问题

一个明显的问题是,在课程级别,您已定义:

 private List<Sample>    samplelist;

然后继续使用

隐藏doSampleList中的变量
 List<Sample> samplelist = new ArrayList<Sample>();

结合将bean标记为@RequestScoped的事实,它将保证samplelist的内容在JSF请求处理期间不一致。

解决

将您的bean标记为@ViewScoped,并根据需要解决变量隐藏问题。

进一步阅读: