Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass value to a java method in EL(using TLD)

 
sammeta Phanikumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm getting the value from request, by passing this value I want to call a java method(getSquere()).

In the jsp where I am calling the java method using prefix and want to pass the value which comes with request to method. I have used EL and <%= %> to get the values and pass an arguemnt but none of them is working. Here is code line which I've used.

${mine:getSquere( ${param.number} )}
${mine:getSquere{ <%= request.getParameter("number") %> }.

Please help me how do Iget it without using scriptlets.

Regards,
Phani
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
creat a tld file to describe your funtion(getSquere).
in the jsp page declare the tag like this
<%@ taglib prefix="mine" uri="/mine.tld" %>

then

<c:set var="name" value="${param.number}"/>
${mine:getSquere{${name}}}

hope this will help
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hongli Li:
creat a tld file to describe your funtion(getSquere).
That will not work. You cannot use the EL to call instance methods at all. EL functions must be static functions and I'm pretty sure that's not what you are describing.

And
${mine:getSquere{${name}}}
This is not valid syntax.

Your best bet is to figure why you need to call a general function in JSP and see if you can get by without it by factoring processing out into the page controller. Otherwise, you might look into an EL function (keeping in mind their limitations) or a custom tag.

Finding something on a JSP page that you can do with a scriptlet but not the EL is usually a red flag that you are doing something in the JSP that should be done in a Java class. Think long and hard about that.
[ March 18, 2008: Message edited by: Bear Bibeault ]
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me revise some typos
${mine:getSquere{${name}}}

should be ${mine:getSquere(name)}
the java class should be something like :



then the tld file

<?xml version="1.0" encoding="UTF-8"?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>Utility functions</description>
<display-name>Utility Tag Library</display-name>
<tlib-version>1.0</tlib-version>
<short-name>utility</short-name>
<function>
<name>mine</name>
<function-class>youupackage.Util</function-class>
<function-signature>java.lang.String
getSuere(java.lang.String)</function-signature>
</function>
</taglib>

then in jsp call
<c:set var="name" value="${param.name}"/>
${mine:getSquere(name)}
[ March 18, 2008: Message edited by: Hongli Li ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(You've still got typos)

It's important to note that EL functions must be static.
[ March 18, 2008: Message edited by: Bear Bibeault ]
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
(You've still got typos)

It's important to note that EL functions must be static.

[ March 18, 2008: Message edited by: Bear Bibeault ]


I used some code I used before, so where is the typo, the method is declared as static. Please point out for me
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should be ${mine:getSquere(name)}

<function>
<name>mine</name>
<function-class>youupackage.Util</function-class>
<function-signature>java.lang.String
getSuere(java.lang.String)</function-signature>
</function>
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hongli Li:
let me revise some typos

should be ${mine:getSuere(name)}
the java class should be something like :



then the tld file

<?xml version="1.0" encoding="UTF-8"?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>Utility functions</description>
<display-name>Utility Tag Library</display-name>
<tlib-version>1.0</tlib-version>
<short-name>utility</short-name>
<function>
<name>mine</name>
<function-class>youupackage.Util</function-class>
<function-signature>java.lang.String
getSuere(java.lang.String)</function-signature>
</function>
</taglib>

then in jsp call
<c:set var="name" value="${param.name}"/>
${mine:getSuere(name)}

[ March 18, 2008: Message edited by: Hongli Li ]


method name is consistent now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic