Sunday, January 06, 2013

JSF 2.1 Tip of the Day: Programmatically Creating a <h:commandButton/>

I have been doing a lot of work on JSF lately including making custom components. It seems like the whole process of creating custom components looks like some arcane arts. It almost seems like black magic in some cases.

I thought I would share some of the things I have discovered over time in an attempt to help others avoid some of the pitfalls I have come across.
This JSF Tip of the Day is an amalgamation of a couple of tips: JSF 2.x Tip of the Day: Programmatically Creating EL ValueExpression and JSF 2.x Tip of the Day: Programmatically Creating EL MethodExpression which are combined with this tip.

In this tip we will create a <h:commandButton/> programmatically. The code from the previous tips noted, are used to create the component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.bluelotussoftware.example.jsf;
 
import com.bluelotussoftware.jsf.utils.JSFUtils;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.Application;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.context.FacesContext;
import javax.inject.Named;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
@Named
@SessionScoped
public class CommandBean implements Serializable {
 
    private static final long serialVersionUID = -7866778404577507805L;
    private HtmlCommandButton commandButton;
    private String UUID;
 
    @PostConstruct
    private void init() {
        commandButton = createCommandButton(FacesContext.getCurrentInstance(),
                "#{commandBean.generateUUID()}", "Generate UUID");
    }
 
    public HtmlCommandButton getCommandButton() {
        return commandButton;
    }
 
    public void setCommandButton(HtmlCommandButton commandButton) {
        this.commandButton = commandButton;
    }
 
    public String getUUID() {
        return UUID;
    }
 
    public void setUUID(String UUID) {
        this.UUID = UUID;
    }
 
    private HtmlCommandButton createCommandButton(final FacesContext context,
            final String methodExpression, final String value) {
        Application application = context.getApplication();
        Class<?>[] clazz = new Class<?>[]{};
        HtmlCommandButton htmlCommandButton =
                (HtmlCommandButton) application.createComponent(HtmlCommandButton.COMPONENT_TYPE);
        htmlCommandButton.setValue(value);
        htmlCommandButton.setActionExpression(JSFUtils.createMethodExpression(methodExpression, String.class, clazz));
        return htmlCommandButton;
    }
 
    public String generateUUID() {
        UUID = java.util.UUID.randomUUID().toString();
        return null;
    }
}
The component is bound using the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title><h:commandButton/> Binding</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputText value="#{commandBean.UUID}"/>
             
 
            <h:commandButton binding="#{commandBean.commandButton}"/>
        </h:form>
    </h:body>
</html>

0 comments :

Popular Posts