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?
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));
And you can place that in the request (for instance) prior to accessing it:
request.setAttribute("uo", UserHandler.getUserObj(Integer.parseInt(id)));
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.
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</tlibvers ion>
<jspversion>2.1</jspversio n>
<function>
<name>userHandler</name>
<function-class>your.packa ge.UserHan dler</func tion-class >
<function-signature>
java.lang.String getUserObj(java.lang.Integ er)
</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.t ld" %>
<c:out value='${myDAO:userHandler (param.vis itor_id) }' />
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</tlibvers
<jspversion>2.1</jspversio
<function>
<name>userHandler</name>
<function-class>your.packa
<function-signature>
java.lang.String getUserObj(java.lang.Integ
</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.t
<c:out value='${myDAO:userHandler
The above is just an example. Of course you want to return a User object.
<function-signature>
package.User getUserObj(java.lang.Integ er)
</function-signature>
</function>
<c:set var="user" value='${myDAO:userHandler (param.vis itor_id) }' />
<function-signature>
package.User getUserObj(java.lang.Integ
</function-signature>
</function>
<c:set var="user" value='${myDAO:userHandler
ASKER
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.JasperEx ception: Method "getUserObj" for function "userHandler" not found in class "com.infrasafe.ivisitor.da taObjects. persistenc e.UserHand ler"
at org.apache.jasper.compiler .DefaultEr rorHandler .jspError( DefaultErr orHandler. java:50)
at org.apache.jasper.compiler .ErrorDisp atcher.dis patch(Erro rDispatche r.java:407 )
at org.apache.jasper.compiler .ErrorDisp atcher.jsp Error(Erro rDispatche r.java:179 )
at org.apache.jasper.compiler .Validator $1MapperEL Visitor.vi sit(Valida tor.java:1 384)
at org.apache.jasper.compiler .ELNode$Fu nction.acc ept(ELNode .java:122)
at org.apache.jasper.compiler .ELNode$No des.visit( ELNode.jav a:193)
at org.apache.jasper.compiler .ELNode$Vi sitor.visi t(ELNode.j ava:234)
at org.apache.jasper.compiler .ELNode$Ro ot.accept( ELNode.jav a:53)
at org.apache.jasper.compiler .ELNode$No des.visit( ELNode.jav a:193)
at org.apache.jasper.compiler .Validator $ValidateV isitor.get FunctionMa pper(Valid ator.java: 1394)
at org.apache.jasper.compiler .Validator $ValidateV isitor.get JspAttribu te(Validat or.java:11 12)
at org.apache.jasper.compiler .Validator $ValidateV isitor.che ckXmlAttri butes(Vali dator.java :937)
at org.apache.jasper.compiler .Validator $ValidateV isitor.vis it(Validat or.java:71 0)
at org.apache.jasper.compiler .Node$Cust omTag.acce pt(Node.ja va:1441)
at org.apache.jasper.compiler .Node$Node s.visit(No de.java:21 63)
at org.apache.jasper.compiler .Node$Visi tor.visitB ody(Node.j ava:2213)
at org.apache.jasper.compiler .Node$Visi tor.visit( Node.java: 2219)
at org.apache.jasper.compiler .Node$Root .accept(No de.java:45 6)
at org.apache.jasper.compiler .Node$Node s.visit(No de.java:21 63)
at org.apache.jasper.compiler .Validator .validate( Validator. java:1489)
at org.apache.jasper.compiler .Compiler. generateJa va(Compile r.java:166 )
at org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:295)
at org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:276)
at org.apache.jasper.compiler .Compiler. compile(Co mpiler.jav a:264)
at org.apache.jasper.JspCompi lationCont ext.compil e(JspCompi lationCont ext.java:5 63)
at org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 03)
at org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:314)
at org.apache.jasper.servlet. JspServlet .service(J spServlet. java:264)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
at org.apache.catalina.core.S tandardWra pper.loadS ervlet(Sta ndardWrapp er.java:11 58)
at org.apache.catalina.core.S tandardWra pper.alloc ate(Standa rdWrapper. java:791)
at org.apache.catalina.core.S tandardWra pperValve. invoke(Sta ndardWrapp erValve.ja va:127)
at org.apache.catalina.core.S tandardCon textValve. invoke(Sta ndardConte xtValve.ja va:174)
at org.apache.catalina.core.S tandardHos tValve.inv oke(Standa rdHostValv e.java:127 )
at org.apache.catalina.valves .ErrorRepo rtValve.in voke(Error ReportValv e.java:117 )
at org.apache.catalina.core.S tandardEng ineValve.i nvoke(Stan dardEngine Valve.java :108)
at org.apache.catalina.connec tor.Coyote Adapter.se rvice(Coyo teAdapter. java:151)
at org.apache.coyote.http11.H ttp11AprPr ocessor.pr ocess(Http 11AprProce ssor.java: 834)
at org.apache.coyote.http11.H ttp11AprPr otocol$Htt p11Connect ionHandler .process(H ttp11AprPr otocol.jav a:640)
at org.apache.tomcat.util.net .AprEndpoi nt$Worker. run(AprEnd point.java :1286)
at java.lang.Thread.run(Threa d.java:595 )
It so happens, however, that com.infrasafe.ivisitor.dat aObjects.p ersistence .UserHandl er is the fully qualified path to the class that contains the method: getUserObj(int). The TLD file I created is below.
Thanks!
I got the following error message:
2008-05-13 16:44:40,859 [StandardWrapperValve] ERROR - Allocate exception for servlet EmployeePopup
org.apache.jasper.JasperEx
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.compiler
at org.apache.jasper.JspCompi
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at org.apache.jasper.servlet.
at javax.servlet.http.HttpSer
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.connec
at org.apache.coyote.http11.H
at org.apache.coyote.http11.H
at org.apache.tomcat.util.net
at java.lang.Thread.run(Threa
It so happens, however, that com.infrasafe.ivisitor.dat
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>
What is your version ?
JSP version is <%= JspFactory.getDefaultFacto ry().getEn gineInfo() .getSpecif icationVer sion()%>
Also please show us the signature of the getUserObj method. It has to be static.
JSP version is <%= JspFactory.getDefaultFacto
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.
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</tlibvers ion>
<function>
<name>userHandler</name>
<function-class>com.infras afe.ivisit or.dataObj ects.persi stence.Use rHandler</ function-c lass>
<function-signature>
com.infrasafe.ivisitor.dat aObjects.U ser getUserObj(java.lang.Integ er)
</function-signature>
</function>
</taglib>
<?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</tlibvers
<function>
<name>userHandler</name>
<function-class>com.infras
<function-signature>
com.infrasafe.ivisitor.dat
</function-signature>
</function>
</taglib>
ASKER
It appears I am running JSP 2.0. The signature of the getUserObj method is as follows:
public static synchronized User getUserObj(int)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked! Thank you so much!
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."