Friday, October 05, 2007

JSF dropdown with BigInteger keys

We wanted to create a drop down with JSF which had keys as java.math.BigInteger and labels as strings. The drop down was to be pre selected with a value. If everything is string, string, thinds work well. However, the drop down value is not prepopulated if it is bound to a BigInteger. Eventually, we were able to solve this with the following code:

JSP




<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">
<%@ page contentType=\"text/html;charset=windows-1252\"%>
<%@ taglib uri=\"http://java.sun.com/jsf/html\" prefix=\"h\"%>
<%@ taglib uri=\"http://java.sun.com/jsf/core\" prefix=\"f\"%>
<f:view>
<html>
<head>
<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=windows-1252\"/>
<title>ConversionTrial</title>
</head>
<body><h:form>
<h:selectOneMenu id=\"menu\" value=\"#{ConversionBean.dto.code}\">
<f:selectItems value=\"#{ConversionBean.valueList}\"/>
</h:selectOneMenu>
</h:form></body>
</html>
</f:view>



Managed Bean



package view;



import java.math.BigInteger;



import java.util.ArrayList;



import javax.faces.model.SelectItem;



public class ConversionBean {

    private DTO dto;

    private ArrayList valueList;

    

    public ConversionBean() {

        dto = new DTO(new BigInteger(\"10\"), \"Large\");

    }

    

    public ArrayList getValueList(){

        valueList = new ArrayList();

        valueList.add(new SelectItem(new BigInteger(\"0\"), \"Small\"));

        valueList.add(new SelectItem(new BigInteger(\"5\"), \"Medium\"));

        valueList.add(new SelectItem(new BigInteger(\"10\"), \"Large\"));

        

        return valueList;

    }

    

    public DTO getDto(){

        return this.dto;

    }

}



DTO




package view;



import java.math.BigInteger;



public class DTO {

    private BigInteger code;

    private String val;

    

    public DTO(BigInteger code, String val) {

        this.code = code;

        this.val = val;

    }

    

    public void setCode(BigInteger s){

        System.out.println(\"setCode to \" + s);

        this.code = s;

    }

    

    public BigInteger getCode(){

        System.out.println(\"getCode returns \" + code);

        return this.code;

    }

}

Labels: , ,

0 Comments:

Post a Comment

<< Home