Paramaters in EL?

R

Robert Mark Bram

Hi All!

Is it possible to do this:



<%String deptName = (String)request.getParameter("deptName");
Department dept = org.getDepartment(deptName); %>


With just c:set or jsp:useBean tags?

Rob
:)
 
R

Ryan Stewart

Robert Mark Bram said:
Hi All!

Is it possible to do this:



<%String deptName = (String)request.getParameter("deptName");
Department dept = org.getDepartment(deptName); %>


With just c:set or jsp:useBean tags?
For the first:
<c:set var="deptName" value="${param['deptName']}"/>

For the second, you can't explicitly call methods using EL, which means you
can't pass an argument to it. However, if you have no better way of doing it,
you might write a sort of lookup bean that has methods like:
public void setDeptName(String deptName) {
this.deptName = deptName;
}

public Department getDepartment() {
return org.getDepartment(this.deptName);
}

Then use:
<jsp:useBean id="lookupBean" ... />
<c:set target="${lookupBean}" property="deptName" value="${param['deptName']}"/>
<c:set var="dept" value="${lookupBean.department}"/>
 
C

Chris Riesbeck

Ryan Stewart said:
Robert Mark Bram said:
Hi All!

Is it possible to do this:

Department dept = org.getDepartment(deptName); %>

With just c:set or jsp:useBean tags?

... you can't explicitly call methods using EL, which means you
can't pass an argument to it. However, if you have no better way of doing it,
you might write a sort of lookup bean that has methods like:
public void setDeptName(String deptName) {
this.deptName = deptName;
}

public Department getDepartment() {
return org.getDepartment(this.deptName);
}

Then use:
<jsp:useBean id="lookupBean" ... />
<c:set target="${lookupBean}" property="deptName" value="${param['deptName']}"/>
<c:set var="dept" value="${lookupBean.department}"/>

Another approach that's a little more work on the class side
but simpler on the JSP side is to have the class create
Map's for the various Map-like getters, so that in the JSP,
you just write

${ org.getDepartmentMap()[deptName] }

There are lots of ways to simplify constructing such Maps.
One way is something like the Mapper utility class below. In
the constructor for your class with getDepartment(), put

Map myDepartmentMap = new Mapper(this) {
public Object get(Object key) {
return ((Organization) getObject()).getDepartment((String) key);
}
};

and add the method

public Map getDepartmentMap() {
return myDepartmentMap;
}

You can do repeat this for any other Map-like methods you have,
e.g., getEmployee().

The version of Mapper below is quite short because it only
supports Map.get(), which is what you need for [] in JSP EL.
Any other operation throws an unsupported operation exception.
It would be easy to add support for put().

package utils;

import java.util.AbstractMap;
import java.util.Set;

public abstract class Mapper extends AbstractMap
{
private Object myObject;

public Mapper(Object obj) {
myObject = obj;
}

public abstract Object get(Object key);

public Object getObject() {
return myObject;
}

public Set entrySet() {
throw new UnsupportedOperationException();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top