Link to home
Start Free TrialLog in
Avatar of infrasafe
infrasafe

asked on

How would I call a static DAO method with JSTL?

I am currently using scriptlets to call a static method of a DAO class (passing in an ID to do a DB lookup) which returns an object of type "user."  Attributes of the returned user object are then displayed on the page (ie. first name, last name, etc).  

I know JSTL is supposed to access properties and not methods but I'm not sure how I would access the properties of a user object before the object has been created.  I know I could use JSTL to instantiate the object but it wouldn't know what to instantiate without first calling that DAO method with the user id.

Below is the current scriptlet code I have for doing this.  Any ideas?
String id = request.getParameter("visitor_id");
User uo = UserHandler.getUserObj(Integer.parseInt(id));

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but I'm not sure how I would access the properties of a user object before the object has been created.  

but the object *would* have been created as you just said above ..?

>> (passing in an ID to do a DB lookup) which returns an object of type "user."
And you can place that in the request (for instance) prior to accessing it:


request.setAttribute("uo", UserHandler.getUserObj(Integer.parseInt(id)));

Open in new window

Avatar of infrasafe
infrasafe

ASKER

The code that I posted is actually the current scriptlet code I have for doing this.  I was wanting to see if the scriptlet code could be replaced with JSTL tags so that the JSP would be completely void of scriptlets.

So basically, the JSTL would need to get the "visitor_id" parameter from the request (this part is obviously not a problem) and then somehow call the DAO method "getUserObj" to retrieve the user object.

I'm really not sure if this is even possible without scriptlets.
>I'm really not sure if this is even possible without scriptlets.  
Yes, it is possible. You could use El custom functions. See "Defining EL functions" in middle page of
http://www.oracle.com/technology/pub/articles/cioroianu_jspapi.html
and see bottom of  
http://java.sun.com/javaee/5/docs/tutorial/doc/bnahq.html   
You could use something like  this for your tld  
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>2.1</jspversion>
  <function>
    <name>userHandler</name>
    <function-class>your.package.UserHandler</function-class>
    <function-signature>
        java.lang.String getUserObj(java.lang.Integer)
    </function-signature>
  </function>
</taglib>
and on your JSP  use something like  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="myDAO" uri="/WEB-INF/myFunction.tld" %>
<c:out value='${myDAO:userHandler(param.visitor_id) }' />
The above is just an example. Of course you want to return a User object.  
    <function-signature>
        package.User getUserObj(java.lang.Integer)
    </function-signature>
  </function>

<c:set var="user" value='${myDAO:userHandler(param.visitor_id) }' />
Thank you ever so much for your suggestions.  I tried what you said and everything seems to be good up to this point...

I got the following error message:

2008-05-13 16:44:40,859 [StandardWrapperValve] ERROR - Allocate exception for servlet EmployeePopup
org.apache.jasper.JasperException: Method "getUserObj" for function "userHandler" not found in class "com.infrasafe.ivisitor.dataObjects.persistence.UserHandler"
      at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
      at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
      at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:179)
      at org.apache.jasper.compiler.Validator$1MapperELVisitor.visit(Validator.java:1384)
      at org.apache.jasper.compiler.ELNode$Function.accept(ELNode.java:122)
      at org.apache.jasper.compiler.ELNode$Nodes.visit(ELNode.java:193)
      at org.apache.jasper.compiler.ELNode$Visitor.visit(ELNode.java:234)
      at org.apache.jasper.compiler.ELNode$Root.accept(ELNode.java:53)
      at org.apache.jasper.compiler.ELNode$Nodes.visit(ELNode.java:193)
      at org.apache.jasper.compiler.Validator$ValidateVisitor.getFunctionMapper(Validator.java:1394)
      at org.apache.jasper.compiler.Validator$ValidateVisitor.getJspAttribute(Validator.java:1112)
      at org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:937)
      at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:710)
      at org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1441)
      at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
      at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
      at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
      at org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
      at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
      at org.apache.jasper.compiler.Validator.validate(Validator.java:1489)
      at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:166)
      at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
      at org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
      at org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
      at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1158)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:791)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:127)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
      at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:834)
      at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
      at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
      at java.lang.Thread.run(Thread.java:595)

It so happens, however, that com.infrasafe.ivisitor.dataObjects.persistence.UserHandler is the fully qualified path to the class that contains the method:  getUserObj(int).  The TLD file I created is below.

Thanks!
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
 
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>2.1</jspversion>
  <function>
    <name>userHandler</name>
    <function-class>com.infrasafe.ivisitor.dataObjects.persistence.UserHandler</function-class>
    <function-signature>
        com.infrasafe.ivisitor.dataObjects.User getUserObj(java.lang.Integer)
    </function-signature>
  </function>
</taglib>

Open in new window

What is your version ?
JSP version is <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()%>  
Also please show us the signature of the getUserObj  method. It has to be static.

>Also please show us the signature of the getUserObj  method.
In the the UserHandler class I mean.
Though its not your current problem, I should have updated my tld for  JSP 2.1  

<?xml version="1.0" encoding="ISO-8859-1" ?>

<taglib
    xsi:schemaLocation=
    "http://java.sun.com/xml/ns/javaee web-
        jsptaglibrary_2_1.xsd"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.1" >
  <tlibversion>1.1</tlibversion>
  <function>
    <name>userHandler</name>
    <function-class>com.infrasafe.ivisitor.dataObjects.persistence.UserHandler</function-class>
    <function-signature>
         com.infrasafe.ivisitor.dataObjects.User getUserObj(java.lang.Integer)
    </function-signature>
  </function>
</taglib>
It appears I am running JSP 2.0.  The signature of the getUserObj method is as follows:
public static synchronized User getUserObj(int)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked!  Thank you so much!