1

so I found this issue, it is really weird..

So I have this xhtml page:

<h:form>
        <rich:dataTable value="#{carsBean.myList}" var="car">
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Name" />
                </f:facet>
                <h:inputText value="#{car.name}">
                    <p:ajax event="keyup" listener="#{carsBean.print}" />
                </h:inputText>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Company" />
                </f:facet>
                <h:inputText value="#{car.company}">
                    <p:ajax event="keyup" listener="#{carsBean.print}" />
                </h:inputText>
            </rich:column>
            <rich:column>
                <f:facet name="header">
                    <h:outputText value="Address" />
                </f:facet>
                <h:inputText value="#{car.address}">
                    <p:ajax event="keyup" listener="#{carsBean.print}" />
                </h:inputText>
            </rich:column>
        </rich:dataTable>
        <h:commandButton value="save-empty">

        </h:commandButton>
    </h:form>

The following is the managedbean:

@ManagedBean(name="carsBean")
@SessionScoped
public class CarsBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7818770780064447805L;
    private List<Car> myList;
    private String test;

    public CarsBean(){}

    public void print(){
        System.out.println("I got hit!");
    }

    /**
     * @return the myList
     */
    @PostConstruct
    void initialiseSession() {
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.getExternalContext().getSession(true);
        if(myList==null){
            myList = new ArrayList<Car>();
        };
        if(myList.isEmpty()){
            for(int a=0;a<10;a++){
                myList.add(new Car("test","test","test"));
            }
        }
    }

    public List<Car> getMyList() {
        return myList;
    }

    /**
     * @param myList the myList to set
     */
    public void setMyList(List<Car> myList) {
        this.myList = myList;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    public static class Car{
        private String name;
        private String company;
        private String address;

        public Car(){}

        public Car(String name, String company, String address) {
            super();
            this.name = name;
            this.company = company;
            this.address = address;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the company
         */
        public String getCompany() {
            return company;
        }
        /**
         * @param company the company to set
         */
        public void setCompany(String company) {
            this.company = company;
        }
        /**
         * @return the address
         */
        public String getAddress() {
            return address;
        }
        /**
         * @param address the address to set
         */
        public void setAddress(String address) {
            this.address = address;
        }
    }
}

Now the aim is to save the users input in the sessionscope such that even if the user closes the tab or makes a new request the data gets persisted.

The above code works, but I have noticed, when I remove the ajax events on the input box it dosent.

Any ideas?

THanks

user1479589
  • 315
  • 1
  • 4
  • 16

1 Answers1

4

The ajax events are causing your changes to be sent to the server, and persisted in the session. If you don't have them, you will need something else to do it. Clasically, you would do a normal post (any submit button on your form will do, really). But your users will have to click the button to explicitly save their changes.

If your goal is really that your users can play around, close their tab, and when they come back everything is as they left it, you will need the ajax events anyway.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
  • Indeed. If your `keyup` event listener was merely for debugging (a method named "print" and the silly implementation suggests that) and you worry about its expensiveness, then use `change` event instead (or just remove `event` attribute altogether; it defaults to `change` already), this way it will only be invoked when the enduser actually blurs the field after change. – BalusC Aug 20 '12 at 13:51
  • @BalusC thanks, I cant believe I am talking to you, I spent the last week trying to implement a datable using backing bean following one of your posts. ANyway- this was a mistake, it is meant to be blur instead and I am really glad my observation was right. But are there any alternatives or do JSF sessions work like this? – user1479589 Aug 20 '12 at 14:03
  • This is not specific to JSF. That's just how HTTP and sessions work. To learn the low level details, read http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 JSF is just sitting on top of basic Servlet API. – BalusC Aug 20 '12 at 14:08
  • @user1479589 this limitation has nothing to do with JSF itself. The session is server-side, so if you want your data to be persisted there, you will need to send it to the server. The alternative is to persist the data client side. For that, look into local storage, or if you really need to, cookies. – Joeri Hendrickx Aug 20 '12 at 14:09