VijayaRagavan
VijayaRagavan

Reputation: 221

Accessing Java method in xpage repeat control

I've created a Java Script Library which has a java class that contains some jdbc code.

It has a method to get values from a database (mysql).

Now i need to access it in the repeat control like <xp:repeat value = ?? >

But i don't find a way to access it there.

If it is a javascript library, the method is accessed as <xp:repeat value="#{javascript:getSQLData()}"

How to achieve it? And is it the right approach to use java in script libraries when we also have a separate application named Java inside the Code Section (below script library in the application view).

My java code is:

package com.db;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List;

public class Db {

Connection con;
Statement st;
ResultSet rs;

public Db(){

    this.connect("localhost", "3306", "vijay", "root", "");
    try {
        this.query("select * from prodet;");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println("///////////////query///////////////////////////////////////////");
        e.printStackTrace();
    }
}

public void connect(String server, String port, String db, String user, String pwd){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager
        .getConnection("jdbc:mysql://localhost:3306/vijay","root", "");
    //con=DriverManager.getConnection("\"jdbc:mysql://"+server+":"+port+"/"+db+"\""+","+"\""+user+"\""+","+"\""+pwd+"\"");
        st=con.createStatement();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

List arr = new ArrayList();


@SuppressWarnings("unchecked")
public void query(String q) throws SQLException{
    rs=st.executeQuery(q);      
            while(rs.next()){
                arr.add(rs.getString(2));
                arr.add(rs.getInt(3));

            }
}

public String getData(){

     String arra = arr.toString();
    return arra;
}

public String doAllDb(){

    return this.getData();
}

public static void main(String a[]){
    Db d = new Db();

        System.out.println(d.getData());
}

}

And the ssjs to access the method is:

importPackage(com.db);

var v = new com.db.Db(); v.doAllDb();

This ssjs is written under Bind data using ssjs.

<xp:repeat id="repeat1" rows="30">
    <xp:this.value><![CDATA[#{javascript:importPackage(com.db);

var v = new com.db.Db(); v.doAllDb();}]]> .

When the xpage is previewed, it is blank. Doesn't show any value. But i tested the java code. It is working fine.

Upvotes: 0

Views: 603

Answers (1)

John Dalsgaard
John Dalsgaard

Reputation: 2807

I use managed beans all the time to do exactly that :-)

<xp:repeat rows="10" var="row" value="#{User.rowClubs}">

"User" is my managed bean. It has to be a Java bean that implements the serializable interface, has a constructor without arguments and has public getters/setters for access to properties. In my example the User bean implements the method getRowClubs() that returns a list of objects representing rows of clubs (from the application I worked on this morning).

Please let me know if you need any more help with managed beans? From your question I guessed you needed help on how to reference methods in your bean.

Edit

I just saw the rest of your bean (in the scrollable view). Using your bean you would use something like:

<xp:repeat id="repeat1" rows="30" value="Db.data" var="row">

assuming that you have defined your bean in faces-config.xml, e.g.:

  <managed-bean>
    <managed-bean-name>Db</managed-bean-name>
    <managed-bean-class>com.db.Db</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>

I would also recommend that you remove your "main" method from the bean. If you need to test it from an ordinary Java program then create a test class with a "main" that instantiates and runs your bean.

/John

Upvotes: 2

Related Questions