0

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>

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

This is the sample of little big form, and the need is, when we select the table row from the bottom, it will be display to top input box for editing purpose.

But when I press the command button it do not work. why? and what is the reason please?

user3463207
  • 35
  • 1
  • 3

1 Answers1

0

Possible Problem

One obvious problem is that at the class level, you've defined:

 private List<Sample>    samplelist;

Then you go ahead and hide the variable in doSampleList with

 List<Sample> samplelist = new ArrayList<Sample>();

Combined with the fact that you have your bean marked as @RequestScoped, it will guarantee that the content of the samplelist will not be consistent during the JSF request processing.

To Solve:

Mark your bean as @ViewScoped instead and resolve the variable hiding problem as you see fit.

Further reading:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104