Making an email link in a table with Java Server Faces

Ok, here’s how to do it.

1. Edit the table with the email in it – and set the type of the email field to a HYPERLINK.

2. Create a STATIC TEXT field on the form somewhere, change its ID to txtJAVASCRIPT, turn off the escape property and add a binding attribute to it. We’ll use this to run a bit of javascript to call the email.

3. Add a binding attribute to tablerowgroup1 on your table

4. In the action code for the Hyperlink insert the following code, this will make the javascript basically tell the browser to open the users email programme with the email address in the subject. Obviously you’ll need to change tblcustomersDataProvider1 to your data provider and email to the field in the database corresponding to the persons email address.

public String hyperlink2_action() {
        // Process email
        tblcustomersDataProvider1.setCursorRow(
                tableRowGroup1.getRowKey());
        String sEmail=tblcustomersDataProvider1.getValue("email")+"";
        if (!sEmail.equals("")) {
           
            txtJAVASCRIPT.setText("<script language='javascript'>\nwindow.location=\"mailto:"+sEmail+"\";\n</script>");
           
        } else {
           
            txtJAVASCRIPT.setText("");
        }
        return null;
    }

5. To cancel the email request we add the following into the public void preprocess() code for the jsf page

    txtJAVASCRIPT.setText(""); 

Using this technique you can pretty much make any javascript happen on a page based on an event.

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on August 27, 2008, in Howto and tagged , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: