Reputation: 17405
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
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
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:
Find Weblogic.xml in your Package Explorer.
open this Weblogic.xml.
Click on JSP Tab of the Weblogic.xml.
On the JSP tab under Output Options(Top Right) unchecked the "Print Nulls" check box.
Build and deploy the application no Null will be displayed in the text box any more.
Upvotes: 1
Reputation: 94653
Use EL
or JSTL
expression. The Expression language is null safe.
Upvotes: 1
Reputation: 18588
testTO.getTest() == null ? "" : testTO.getTest()
try this , it will surely work.
Upvotes: 3
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
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