File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes pass value to a java method in EL(using TLD) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "pass value to a java method in EL(using TLD)" Watch "pass value to a java method in EL(using TLD)" New topic
Author

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

sammeta Phanikumar
Ranch Hand

Joined: Oct 25, 2007
Posts: 81
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


SCJP 5, SCWCD 5, SCDJWS 5
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
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


Do you know why this cup is useful? Because it is empty.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hongli Li
Ranch Hand

Joined: Oct 29, 2006
Posts: 124
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

Joined: Jan 10, 2002
Posts: 60780
    
  65

(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

Joined: Oct 29, 2006
Posts: 124
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

Joined: Jan 10, 2002
Posts: 60780
    
  65

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

Joined: Oct 29, 2006
Posts: 124
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: pass value to a java method in EL(using TLD)
 
Similar Threads
JSTL query: Passing value from xml document into Java
Passing values to param value dynamically in jsp
Doubt in setAttribute() for Request object
commandLink inside dataTable not working
How to pass parameter to JSF backing Bean