Wednesday, December 9, 2009

Object instantiation with JSTL and JSP

Have you ever wanted to instantiate an object and use them in your JSTL expressions? In my case, I wanted to surround an object with a wrapper class that provided extra functionality to the objected being wrapped, and be able to access the wrapped object from within JSTL expressions.

To do this I used a combination of JSP and JSTL. I used JSP tags to instantiate the wrapper class and JSTL to assign the object being wrapped as follows:

                <jsp:useBean id="strainWrapper" class="" />                    
                <c:set target="${strainWrapper}" property="strain" value="${mouse.strain}" />

public class StrainHelperWrapper {

    private Strain strain; 
    public StrainHelperWrapper() {}
    public StrainHelperWrapper(Strain strain) {
        this.strain = strain;
    public Strain getStrain() {
        return strain;
    public void setStrain(Strain strain) {
        this.strain = strain;

     * Returns a list of Amplifluor requirements for the Genotyping Assays
     * @return
    public String getAmplifluors() {
        StringBuilder sb = new StringBuilder();
        if (strain != null) {
            List<Genotype> genotyping = strain.getGenotypes();
            if (genotyping != null) {
                int counter = 0;
                for (Genotype genotype: genotyping) {
                    if (counter > 0)
                        sb.append(", ");
                    String yesNo = genotype.getAmplifluor() ? "Yes" : "No";
        return sb.toString();

1 comment: