Blog Archives

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

Faces Grid selecting a single Row

How to make a Java Faces Table select just one row.
 
Thanks to Sun Developer Support for this – except for they left out an iddy bit that would stoping working.  By the way Sun Developer Support is definitely worth the money.
 
1.Create a new Web Application project, name it Row, and enable the Visual Web JavaServer Faces framework
 
2.Drag and drop a table component and a button on to the visual designer.

3.From the Servers tab drag and drop Data Sources->Travel->Tables->Person on to the table component.

4.Right Click on the Table Component and Select Table Layout.
Click on the “New” button to add a new column.
Using “Up” button move the newly added column to the top of the list.
Remove the header text.
Select Radio Button for component type and remove the text in Value Expression Field.

5.Add the following code to the page bean:
 public String getCurrentRow() {
        return tableRowGroup1.getRowKey().getRowId();
    }
  
    public void setCurrentRow(int row) {
    }
  
    private Object lastSelected=”0″;
  
    public Object getRBSelected() {
        String sv = (String)radioButton1.getSelectedValue();
        return sv.equals(lastSelected) ? sv : null;
    }
  
    public void setRBSelected(Object selected){
        if (selected != null) {
            lastSelected = selected;
        }
    }

6. In either the Outline window or the Visual Designer, select radioButton1.

7. In the Data section for radioButton1, click the … button for the selected property. A dialog box appears.
Select Use Binding, Click the Bind to an Object tab, select  RBSelected, and click OK.
The application will now use the getRBSelected() and setRBSelected() methods to display and save user input for this component.
Now change the Name property to buttonGroup
8.In the Advanced section of the Properties sheet, click the … button for the selectedValue property.
Select Use Binding, Click the Bind to an Object tab, select  currentRow, and click OK.
The application will now use the getCurrentRow() method to return the selected value for this component.

9.Drag a static text component on to the visual designer and add the following code in a button action method:

public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        String aRowId = (String)RadioButton.getSelected(
                “buttonGroup”);
        RowKey aRowKey = personDataProvider.getRowKey(aRowId);
      staticText1.setText(“You Selected ” +
                personDataProvider.getValue(“PERSON.NAME”,aRowKey));
        return null;
    }

10.Build and run the application.