webtier@glassfish.java.net

Re: [webtier] Ajax request+onclick

From: Jim Driscoll <Jim.Driscoll_at_Sun.COM>
Date: Wed, 29 Jul 2009 13:51:57 -0700

Well, I'm able to say that the ajax stuff is working perfectly.

There's at least one problem with your code, though, that I can see.

I'm not really an expert, but I can tell you that you shouldn't bind to
anything that isn't request scoped - among other problems, there's
thread safety to look out for - UIComponent isn't thread safe.

Also, I'm not really clear as to what you think the ajax call should be
updating, based on this small example - you don't actually have it
updating anything, so no state changes on the server side. But yes,
this example works perfectly, updating the datatable via ajax (to have
the same value as before, but updating nonetheless).

Jim

On 7/29/09 10:17 AM, webtier_at_javadesktop.org wrote:
> [b]xhtml[/b]:
>
> <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:ui="http://java.sun.com/jsf/facelets">
> <h:head>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> <title></title>
> </h:head>
> <h:body>
> <h:form id="form1" prependId="false">
>
> <h:outputScript name="jsf.js" library="javax.faces" target="head" />
>
> <h:dataTable width="100%" id="dataTable" binding="#{testBean.dataTable}" value="#{testBean.numbers}" var="number">
> </h:dataTable>
>
> </h:form>
>
> </h:body>
> </html>
>
> [b]bean[/b]:
>
>
>
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.annotation.PostConstruct;
> import javax.faces.bean.ManagedBean;
> import javax.faces.bean.SessionScoped;
> import javax.faces.component.UIColumn;
> import javax.faces.component.UIData;
> import javax.faces.component.html.HtmlCommandButton;
> import javax.faces.component.html.HtmlDataTable;
> import javax.faces.context.FacesContext;
>
> @ManagedBean
> @SessionScoped
> public class TestBean implements Serializable {
>
> private static final long serialVersionUID = -7299773608161438216L;
>
> private UIData dataTable;
>
> private List<Integer> numbers = new ArrayList<Integer>();
>
> public List<Integer> getNumbers() {
> return numbers;
> }
>
> public UIData getDataTable() {
> return dataTable;
> }
>
> public void add() {
> numbers.add(4);
> System.out.println("add");
> }
>
> @PostConstruct
> public void init() {
> dataTable = new HtmlDataTable();
> UIColumn col = null;
>
> numbers.add(1);
> numbers.add(2);
> numbers.add(3);
>
> col = new UIColumn();
> dataTable.getChildren().add(col);
>
> HtmlCommandButton numButton = new HtmlCommandButton();
> numButton.setValueExpression("value", FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{number}", String.class));
> numButton.setActionExpression(FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{testBean.add}", String.class, new Class<?>[] {}));
> numButton.setOnclick("jsf.ajax.request(this, event, {execute: this.id, render: 'dataTable'}); return false;");
> numButton.setValue("add");
>
> col.getChildren().add(numButton);
>
> }
>
> }
> [Message sent by forum member 'gabox01' (gabox01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=358097
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>