370 Lotus blogs updated hourly. Who will post next? Home | Downloads | Events | Jobs | Twitter | Bookmarks | Pods | Forum | Blogs | Search | myPL | About 
 
May 23, 2012, 06:11:23 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Calendar Login Register  
Pages: [1]
  Print  
Author Topic: JSF JavaBean does not work for Xpages (What Gives)  (Read 950 times)
0 Members and 1 Guest are viewing this topic.
osc_dev
Newbie
*
Offline Offline

Posts: 3


View Profile
« on: April 14, 2011, 10:47:11 AM »

Hey Gang!

Looks like I should be able to treat Xpages pretty much like JSF when writing beans and so on...

I am concerned about a certain JavaBean that works well in App built on Eclispe Ganymede, but does not work in Xpages.  Would you have a look and see what I may be missing.

I will simply post the URL where I got the code, kudos by the way to the person who wrote it: http://srikanthtechnologies.com/blog/java/xml_jsf_datatable.html

Here is the XML:

Code:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
        <xp:dataTable rows="5" id="bookTable" var="currentBook" value="#{Books.books}"
        style="width:50%">
        <xp:this.facets>
        <xp:pager layout="Previous Group Next" xp:key="header"
        id="pager1" for="bookTable">
        </xp:pager>
        <xp:pager layout="Previous Group Next" xp:key="footer"
        id="pager2" for="bookTable" partialRefresh="true">
        </xp:pager>
        </xp:this.facets>
        <xp:column id="titleColumn">
        <xp:text escape="true" id="titleField"
        value="#{currentBook.title}">
        </xp:text>
        </xp:column>
        <xp:column id="authorColumn">
        <xp:text escape="true" id="authorField"
        value="#{currentBook.author}">
        </xp:text>
        </xp:column>
        <xp:column id="priceColumn">
        <xp:text escape="true" id="priceField"
        value="#{currentBook.price}">
        </xp:text>
        </xp:column>
        </xp:dataTable>
</xp:view>






JavaBean
+++++++++++++++++++++++++++++++++++++++++++++++++++>>
Code:

public class Book {
    private String title, author;
    private int price;

    public Book() {}
    public Book(String title, String author, int price) {
        this.title= title;
        this.author = author;
        this.price = price;
    }

  public String getTitle() {
      return title;
  }

  public void setTitle(String title) {
      this.title = title;
  }

  public String getAuthor() {
      return author;
  }

  public void setAuthor(String author) {
      this.author = author;
  }

  public int getPrice() {
      return price;
  }

  public void setPrice(int price) {
      this.price = price;
  }
}



Backing Bean

+++++++++++++++++++++++++++++++++++++++++++++>>
Code:
import java.io.File;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Books {

    public ArrayList<Book> getBooks() {

        ArrayList<Book> al = new ArrayList<Book>();
        Book b;
       
        // read data from XML file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document  document;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();

            // get physical path for BOOKS.XML. First get access to ServletContext using FacesContext
            ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
           
            String filepath = context.getRealPath("books.xml");  // get physical path for /books.xml

            document = builder.parse( new File(filepath));
            Element root = document.getDocumentElement();

            NodeList books  =  root.getChildNodes();

            for (int i = 0 ; i < books.getLength(); i ++)
            {
                // skip the rest if node is not an elemtn
             if ( books.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
               
                NodeList bookdetails = books.item(i).getChildNodes();
               
                // create a book with the data from book element in XML document
                b = new Book( bookdetails.item(1).getTextContent(),
                                   bookdetails.item(2).getTextContent(),
                                   Integer.parseInt(bookdetails.item(3).getTextContent())) ;
                al.add(b);  // add book to ArrayList
            }
            return al;
        }
        catch (Exception e) {
           System.out.println("\n** Parsing error" + ", line " + e.getMessage());
           return null;
        }
    }
}




WebXML

++++++++++++++++++++++++++++++++++++++++++++++++>>
Code:
  <managed-bean>
    <managed-bean-name>Books</managed-bean-name>
    <managed-bean-class>com.osc.blackberry.test.Books</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>

What are your thoughts, I tried everything; could be my lack of Xpages knowledge here... Any help you can provide is helpful...

Köll
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!