aspose file tools*
The moose likes JSP and the fly likes JSTL Syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL Syntax" Watch "JSTL Syntax" New topic
Author

JSTL Syntax

latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
Please let me know the equivalent syntax of the following code in JSTL. I have already imported all the JSTL libraries.

display.jsp contains the following code.
--------------------
String loginErrorMsg = "Invalid User ID / Password ! Please try again !";
%><%= loginErrorMsg %><%

I tried replacing the expression with the following but it didn't work:
<c:out value="${loginErrorMsg}"/>

Please let me know the correct JSTL syntax.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

If loginErrorMsg is a scripting variable, you can't. Why would you have scripting variables in a page using JSTL and EL? Use either the JSTL and EL, or resort to discredited scriptlets, but don't mix them.

If you want to set the message as a scoped variable (which seems pointless -- if you're going to hard-code it into the page, why bother with variables?), use <c:set>.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
Bear Bibeault wrote:If loginErrorMsg is a scripting variable, you can't. Why would you have scripting variables in a page using JSTL and EL? Use either the JSTL and EL, or resort to discredited scriptlets, but don't mix them.

If you want to set the message as a scoped variable (which seems pointless -- if you're going to hard-code it into the page, why bother with variables?), use <c:set>.


Thanks for the quick response, Bear.

This is what I tried and it worked for me.
<c:set var="loginErrorMsg" value="Invalid User ID / Password ! Please try again !" />
<c:out value="${loginErrorMsg}" />

But this is what I understood from your suggestion.
1) I should never declare any variable in JSP. Instead it should come from my controller servlet as a scoped variable using request.setAttribute("loginErrorMsg", loginErrorMsg);
2) Then I can use that variable for displaying error message using <c:out value="${requestScope.loginErrorMsg}" />

Please let me know if I have understood you correctly.

Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Yes. Except for point #2, (assuming JSP 2.0) you don't need to use <c:out> for strings you know do not need HTML encoding. You can just use the EL expression.

You should use <c:out> whenever you do not know the string is safe, or is a value entered by a user.
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
I am converting my entire JSP code from scriptlet to JSTL. This is a new issue that I faced.

${pu:getParameter(pageContext.request, "validLogin")}

When I call the above code it says me:
org.apache.jasper.JasperException: The class HttpServletRequest request specified in the method signature in TLD for the function pu:getParameter cannot be found. HttpServletRequest request
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)


I am trying to use a static method "getParameter" of a class which is as follows:
public final class ParamUtils
{
private ParamUtils() {} // so cannot new an instance

public static String getParameter(HttpServletRequest request, String pName)
{
String pValue = GenericUtils.nullToEmptyString(request.getParameter(pName));
GenericUtils.out(pName + " = " + pValue);
return pValue;
}
}

Where am I going wrong? Please advise
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
latin chandel wrote:I am converting my entire JSP code from scriptlet to JSTL. This is a new issue that I faced.

${pu:getParameter(pageContext.request, "validLogin")}

When I call the above code it says me:
org.apache.jasper.JasperException: The class HttpServletRequest request specified in the method signature in TLD for the function pu:getParameter cannot be found. HttpServletRequest request
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)


I am trying to use a static method "getParameter" of a class which is as follows:
public final class ParamUtils
{
private ParamUtils() {} // so cannot new an instance

public static String getParameter(HttpServletRequest request, String pName)
{
String pValue = GenericUtils.nullToEmptyString(request.getParameter(pName));
GenericUtils.out(pName + " = " + pValue);
return pValue;
}
}

Where am I going wrong? Please advise


I think I got it. I replaced my code with the following:
<c:if test="${param.validLogin == 'false'}">
<c:set var="loginErrorMsg" value="Invalid User ID / Password ! Please try again !" />
${loginErrorMsg}
</c:if>

But I am having couple of queries:
1) Does the implicit "param" object handles null values also? I am asking this because in traditional scriptlet if we want to fetch request parameter value then we do something like:
if (request.getParameter("validLogin") != null && request.getParameter("validLogin").length() > 0)
{
String validLogin = request.getParameter("validLogin");
}
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
latin chandel wrote:
latin chandel wrote:I am converting my entire JSP code from scriptlet to JSTL. This is a new issue that I faced.

${pu:getParameter(pageContext.request, "validLogin")}

When I call the above code it says me:
org.apache.jasper.JasperException: The class HttpServletRequest request specified in the method signature in TLD for the function pu:getParameter cannot be found. HttpServletRequest request
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)


I am trying to use a static method "getParameter" of a class which is as follows:
public final class ParamUtils
{
private ParamUtils() {} // so cannot new an instance

public static String getParameter(HttpServletRequest request, String pName)
{
String pValue = GenericUtils.nullToEmptyString(request.getParameter(pName));
GenericUtils.out(pName + " = " + pValue);
return pValue;
}
}

Where am I going wrong? Please advise


I think I got it. I replaced my code with the following:
<c:if test="${param.validLogin == 'false'}">
<c:set var="loginErrorMsg" value="Invalid User ID / Password ! Please try again !" />
${loginErrorMsg}
</c:if>

But I am having couple of queries:
1) Does the implicit "param" object handles null values also? I am asking this because in traditional scriptlet if we want to fetch request parameter value then we do something like:
if (request.getParameter("validLogin") != null && request.getParameter("validLogin").length() > 0)
{
String validLogin = request.getParameter("validLogin");
}


2) How can I call my static method which converts String to boolean value in the following JSTL code?
<c:if test="${param.validLogin == 'false'}">

My static method is:
public static boolean stringToBool(String str)
{
boolean retValue = false;
str = nullToEmptyString(str);

if (str.length() > 0)
{
if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes") || str.equalsIgnoreCase("y"))
retValue = true;
else if (str.equalsIgnoreCase("false") || str.equalsIgnoreCase("no") || str.equalsIgnoreCase("n"))
retValue = false;
}
return retValue;
}

Instead of comparing string value "false" I want to compare the boolean value after passing through the above method:
Can I do something like?

<c:if test="${pu:stringToBool(param.validLogin) == false}">



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Does the implicit "param" object handles null values also?


Yes:

How can I call my static method

Static methods can be defined as EL functions.
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
Hi I am getting the following error while calling a static method from a class using JSTL.

org.apache.jasper.JasperException: The class java.lang.String str specified in the method signature in TLD for the function u:stringToBool cannot be found. java.lang.String str

<pre>

My index.jsp
---------------
<%@taglib uri="/WEB-INF/tlds/utils.tld" prefix="u" %>
<c:if test="${u:stringToBool(param.validUser)}"> =====> Gives Error


My web.xml
----------------
<taglib>
<taglib-uri>http://devsphere.com/articles/calltag/utils.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/utils.tld</taglib-location>
</taglib>

My Utils.tld
----------------
<taglib version="2.0" 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 web-jsptaglibrary_2_0.xsd">

<tlib-version>1.0</tlib-version>
<short-name>u</short-name>
<uri>/WEB-INF/tlds/utils</uri>

<function>
<name>stringToBool</name>
<function-class>jstl.GenericUtils</function-class>
<function-signature>boolean stringToBool(java.lang.String str)</function-signature>
</function>

</taglib>



GenericUtils.java
----------------------
public static boolean stringToBool(String str)
{
return (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes")) ? true: false;
}

</pre>

Please advise.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

The taglib declaration in the web.xml is ugly. I'd use a decent URI for the TLD and dispense with any web.xml declaration.

boolean is not a valid type to use for the return value. No primitive. Use java.lang.Boolean.
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
Still doesn't work. Gives the same error. Even tried chaning the uri as you suggested but no result.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Please post the updated code fragments. Use code tags. I will not look at any more code that is not code tagged.
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11


New to forum hence dont have much idea how to use it. Apologize for the incovenience.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

I repeat: give the TLD a real URI, and remove the taglib declaration from the web.xml. Use the real URI in the JSP's taglib declaration.

It may not be the source of any problems, but needless and unnecessary configuration just make things more complicated than they need to be and have a habit of gumming up the works.

Is java.lang.boolean a legal class name?

Attention to detail is key if you want to get things working.
latin chandel
Greenhorn

Joined: Nov 13, 2009
Posts: 11
I got it Thanks a lot Bear.

Committed two mistakes.
1) Was using java.lang.boolean
2) In function signature I was using "java.lang.String str" in my code

Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL Syntax