Ganesh Rathinavel
Ganesh Rathinavel

Reputation: 1335

Capture the value of JSP Expression Language variable into a java variable

is it possible to capture the value of JSP Expression Language variable into a java variable in a jsp page?

Eg: I have a variable ${error.type} and i want to capture the value of ${error.type} into a java variable, say something like this: String errortype = ${error.type} (I know this isn't possible) Is there anyway for doing this? Actually I want to check if ${error.type} is null or not, if there is any value available I will show success message else an error message. Thanks I'm new to java and JSP :)


Update: I tried this code

 <c:choose>
     <c:when test="${not empty error.type}"> 
        test error message    
     </c:when>     
     <c:when test="${empty error.type}">
         test success message     
     </c:when> 
 </c:choose> 

But now both the text are displayed simultaneously irrespective of empty or not! Even i tried ${errors.errors ? "some text when true" : "some text when false"} and guess what, every time i'm getting "some text when false" printed in browser though the value is getting initialized correctly. any idea why?

Upvotes: 0

Views: 996

Answers (2)

Ganesh Rathinavel
Ganesh Rathinavel

Reputation: 1335

i figured it out. i didn'd add <%@taglib uri="java.sun.com/jsp/jstl/core"; prefix="c" %> in the beginning of the jsp page. I didn't know we need to initialize it:) Anyway now it's working fine! thanks

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Actually I want to check if ${error.type} is null or not, if there is any value available I will show success message else an error message

You're looking for ${empty error.type} in order to know if error.getType() is null:

<c:choose>
    <c:when test="${not empty error.type}">
        ${error.message}
    </c:when>
    <c:when test="${empty error.type}">
        <!-- show your valid message -->
    </c:when>
</c:choose>

Upvotes: 2

Related Questions