Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Paramaters in EL?

Thread view: 
Robert Mark Bram - 20 Mar 2005 06:15 GMT
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
:)
Ryan Stewart - 20 Mar 2005 21:26 GMT
> Hi All!
>
[quoted text clipped - 4 lines]
>
> 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}"/>
Chris Riesbeck - 21 Mar 2005 23:21 GMT
> > Hi All!
> >
[quoted text clipped - 19 lines]
> <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();
 }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.