Skip navigation
2010

Ilya Sorokoumov's Blog

December 2010 Previous month Next month
I didn't know anything about this workaround for a long time and now I even can't remember where I had finally read it. But anyway it can help you if you want to call some function on JSF page with parameter(or even with several parameters) when page is being rendered.
So let's imagine that you want to perform some simple transformation on data which you just got from a bean.
Most likely you want to write an expression like this:
#{myBean.performTransform(myBean.data)}
But this won't work in JSF 1.2. It will work in JSF 2.0 of course but what if you already have a giant application on JSF 1.2.
So finally I found a little bit strange but still very helpful trick. You should write a class which implements java.util.Map interface and you should leave all method except of get method empty. This get method should implement a transform you want to perform. And then you can create an instance of this class and use it in JSF EL on jsp pages. My own code is below:

I didn't know anything about this workaround for a long time and now I even can't remember where I had finally read it. But anyway it can help you if you want to call some function on JSF page with parameter(or even with several parameters) when page is being rendered.

So let's imagine that you want to perform some simple transformation on data which you just got from a bean.

Most likely you want to write an expression like this:

#{myBean.performTransform(myBean.data)}

But this won't work in JSF 1.2. It will work in JSF 2.0 of course but what if you already have a giant application on JSF 1.2.

So finally I found a little bit strange but still very helpful trick. You should write a class which implements java.util.Map interface and you should leave all method except of "T get(Object key)" method empty. This "T get(Object key)" method should implement a transform you want to perform. And then you can create an instance of this class and use it in JSF EL on jsp pages like this:

 

#{myBean.performTransform[myBean.data]}

 

My own code is below:

 

 

DummyMap.java:

 

 

import java.util.Collection;

import java.util.Map;

import java.util.Set;

 

public abstract class DummyMap<T> implements Map<String, T> {

 

    @Override

    public abstract T get(Object key);

 

    @Override

    public int size() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public boolean isEmpty() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public boolean containsKey(Object key) {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public boolean containsValue(Object value) {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public T put(String key, T value) {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public T remove(Object key) {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public void putAll(Map<? extends String, ? extends T> m) {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public void clear() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public Set<String> keySet() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public Collection<T> values() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

    @Override

    public Set<Entry<String, T>> entrySet() {

        throw new UnsupportedOperationException("Not supported yet.");

    }

 

}

 

 

MyBean.java:

 

 

public class MyBean {

 

    private String data = "123";

 

    private DummyMap<String> performTransform = new DummyMap<String>() {

        @Override

        public String get(Object key) {

            if (key != null) {

                return "Hello " + key + "!";

            } else {

                return null;

            }

        }

    };

 

    public DummyMap<String> getPerformTransform() {

        return performTransform;

    }

 

    public void setPerformTransform(DummyMap<String> performTransform) {

        this.performTransform = performTransform;

    }

 

    public String getData() {

        return data;

    }

 

    public void setData(String data) {

        this.data = data;

    }

 

}

 

 

So this workaround does not looks right but it can help a lot especially when you have an iteration tag like h:dataTable and you want to perform some simple transfrom on data of current row. I also don't suggest anybody to use a complicated logic or access to DB in your get method either.

 

If someone has any other ideas or approaches for JSF 1.2 which can call functions with parameters from a jsp page, please, write it in comments.

 

P.S. I guess that the same is possible with custom EL functions but I've never seen articles about their creation.

I'm a J2EE developer and I have about 2,5 years of experience in developing enterprise applications. But recently I became a team leader in my company and that's why I decided that I should pay more attention on communication with people and exchange of experience and best practices. In this blog I'm going to raise topics which I want to discuss. I appreciate any comment and suggestion even if they would not be pleasant for me. I also appologize about my English. It's still not good enough but I promise to keep working on it. =)
These are some of the topics which I want to raise in this blog:
1) comparing of IDEs
2) JSF + Richfaces best practices, work arounds, and tricks
3) comparing of application servers
4) JPA, Hibernate, etc.
5) Improving of performance in J2EE
6) arhitecture of J2EE applications
I think that this list will be updated later. If you want to discuss something with me you can also write it in this topic's comments and I'll probably create a new article for it.

I'm a J2EE developer and I have about 3 years of experience in developing enterprise applications(I had a few years of developing smaller applications before it). But recently I became a team leader in my company and that's why I decided that I should pay more attention to communication with people and exchange of experience and best practices. In this blog I'm going to raise topics which I want to discuss. I appreciate any comments and suggestions even if they would not be pleasant for me. I also apologize about my English. It's still not good enough but I promise to keep working on it. =)

These are some of the topics which I want to raise in this blog:

1) comparing of IDEs

2) JSF + Richfaces best practices, work arounds, and tricks

3) comparing of application servers

4) JPA, Hibernate, etc.

5) Improving of performance in J2EE

6) arhitecture of J2EE applications

I think that this list will be updated later. If you want to discuss something with me you can also write it in this topic's comments and I'll probably create a new article for it.

Filter Blog

By date:
By tag: