Vicky
Vicky

Reputation: 17405

handling null values in text box in jsp

Say a text box takes value from Transfer Object as below in a jsp:

<INPUT TYPE="text" NAME="test" ID="test" value="<%=testTO.getTest()%>">

However, if getTest returns null, null will be displayed.

How to use ? : with scriptlet so that if the value is null, blank is displayed else the value returned from TO.

Upvotes: 2

Views: 10870

Answers (6)

Ahmad Musa
Ahmad Musa

Reputation: 78

use it like this for printing blank:

<INPUT TYPE="text" NAME="test" ID="test" value="<%= ((testTO.getTest()==null)?"":testTO.getTest()) %>">

ok add this condition

(&& testTO.getTest().length() == 0)

if it is already returning a String if not you must use this

(&& testTO.getTest().toString().length() == 0)
<INPUT TYPE="text" NAME="test" ID="test" value="<%= ((testTO.getTest()==null && testTO.getTest().length() == 0)?"":testTO.getTest()) %>">

Upvotes: 0

Sagar S. Kaplay
Sagar S. Kaplay

Reputation: 11

To hide null spaces in JSP :-

I am not sure will it help you or not but it worked well in my case.

I am using web-logic 10.3 and the jsp in which I want to hide nulls is part of my web application.

Steps:

  1. Find Weblogic.xml in your Package Explorer.

  2. open this Weblogic.xml.

  3. Click on JSP Tab of the Weblogic.xml.

  4. On the JSP tab under Output Options(Top Right) unchecked the "Print Nulls" check box.

  5. Build and deploy the application no Null will be displayed in the text box any more.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94653

Use EL or JSTL expression. The Expression language is null safe.

Upvotes: 1

dku.rajkumar
dku.rajkumar

Reputation: 18588

testTO.getTest() == null ? "" : testTO.getTest()

try this , it will surely work.

Upvotes: 3

noname
noname

Reputation: 1

if(testTO.getTest()!=null) {    
out.print("<input type=\"text\" name=\"test\" id=\"test\" value=\""+testTO.getTest()+"\">");}
else
{
out.print("<input type=\"text\" name=\"test\" id=\"test\" value=\"\">");
}

Upvotes: 0

mgaert
mgaert

Reputation: 2398

If your're using WebLogic Server, there's a setting (see Using the WebLogic JSP Compiler)

-noPrintNulls

Shows "null" in jsp expressions as "".

which prevents the null in this case. Your container may have a similar feature. Advantage: no code change required. I think this can also be set in weblogic.xml.

Upvotes: 2

Related Questions