Search code examples
jspjavabeansel

How to get a variable's value from a Java Bean in JSP using Expression Language?


I have been looking around to get a variable's value that I am populating at the back end and display it in a JSP using Expression Language. I am not using servlets so I cannot call .getParameter(){} and it is also not part of URL. I am not sure enough if this is possible. Suppose I have the following bean.

public class Test {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String toString() {
    setName( "Expression Language" );
    return this.getName();
}   

}

JSP

<div id="showName">
    <c:if test="${name != null}">
        <td class="fieldLabelBold" style="color:#0033CC" 
        align="center">${name}</td>
    </c:if>
</div>

Can I do something like above code snippet.

Thanks....


Solution

  • You need to declare a bean using your 'Test' class:

    <jsp:useBean id="testBean" class="package.Test"> 
    

    Here you can find a good tutorial about JSP (and beans)