I'm not sure if I can do this, but I'm trying to print a string with Expression Language only (no JSTL). The string is going to often-times be null, and I want an empty string printed to HTML instead of the word "null". This is what I'm trying (without success):
<%
myString = "";
myString = someMethodThatMayReturnNull(); // returns string or null
%>
<b>${myString}</b><br />
<b>${ (empty myString) ? "myString-is-empty" : "myString-is-NOT-empty" }</b><br />
The reason I don't want to use JSTL is because I'm trying to make the minimum changes to a fairly good sized system.
Scriptlets and EL doesn't share the same variable scope. You need to prepare EL variables as attributes of page, request, session or application scope. As you have right now, it is always empty.
<%
myString = "";
myString = someMethodThatMayReturnNull(); // returns string or null
request.setAttribute("myString", myString); // It's now available by ${myString}
%>
Note that you normally do that Scriptlet part inside a Servlet. Also note that EL is null-safe, so anything which is truly null, i.e. if (object == null) passes, then it simply won't be printed at all.
Or, if your concrete problem is that it prints literally ${myString} and so on in the browser, while it worked with JSTL <c:out>, then it means that you aren't using a JSP 2.0 compatible servletcontainer or that your webapp's web.xml is not declared conform at least Servlet 2.4, or that you have isElIgnored set to true. Verify and fix it all.
EL print the variables only when its available in pageContext scope. Here the variables are in page scope. If the variable is in some other scope, you have to specify the scope while using it. Do something like this to print the value of 'mystring' variable.
<%
myString = "stackoverflow";
pageContext.setAttribute("myString",myString);
%>
${myString}
It will print the value of myString, if myString is 'null' it will print empty string(nothing will be printed).
suppose if put the variable in request scope, you have to use "requestScope"
<%
myString = "stackoverflow";
request.setAttribute("myString",myString);
%>
${requestScope.myString}
why do u want to check String null in jsp code?
If you do not want to use scriptlet then you can check string null in mentod itself and return empty string in case of null.
myString = someMethodThatMayReturnNull(); // it should never return null.
And method is from other code then you can wrap this with your java code.
Related
I have a scenario in which i am rendering a jsp page .One of my value is in my model Form entity.I am assigning it in one of my form input like below :
<s:hidden name="myModelName.myUserName"/>
I am trying to assign it to my java variable inside my jsp.I tried accessing the model directly like this
<%= String myName = myModelName.myUserName %>
But i get error message "myModelName cannot be resolved.I have then tried accessing from the hidden field.But i dont know how to use it.Anyway i need the value in java variable inside jsp for some reason.Anyone help me out how to do it
You can do something like this:
assign your model property value to variable userName
<c:set var="userName" scope="session" value="${myModelName.myUserName}"/>
and then you can output it using this tag:
<c:out value = "${userName}"/>
You can read more about JSTL tags here: Using JSTL tags
Hope this helps. Ask if you need anything else.
I got the following code kind of inside of my <% %> in my jsp file.
Two problems here:
Why doesnt my breakpoint doesnt stop in those lines?
Why does have a nullpointerException happen when i use these variables somewhere inside my jsp like these <%=beneficiariesList%>. This value debugging in eclipse in the display view says " beneficiariesList cant be resolved" . For example tipoBeneciarioDatosClientes says the value which is "XXXX"
<% ...... bla bla bla
String tipoBeneficiarioDatosClientes = "XXXXX";
String beneficiariesList = "XXXXX";
if (null != polizaBean.xxxxx() && !polizaBean.getTipoBeneficiario().isEmpty()) {
tipoBeneficiarioDatosClientes = polizaBean.xxxxxx();
if(tipoBeneficiarioDatosClientes.equalsIgnoreCase("xxxxx")) {
JSONArray beneficiaries = JSONArray.fromObject(polizaBean.xxxxx());
beneficiariesList = beneficiaries.toString();
}
}
%>
You have a NullPointerException because variables which you are using inside Scriptlets <% ... %> are only available inside that scriplet.
If you want to declare variable which will be available in Expressions <%= %> you need to declare them inside a Declations block <%! %>.
From JSP 2.0 Specification :
Declarations are used to declare variables and methods in the
scripting language used in a JSP page.
...
Declarations are initialized when the JSP page is initialized and are made
available to other declarations, scriptlets, and expressions.
I am new in jsp domain and I don't know how to attribute a String value from a message because it give me error
<%String mesg=%>
<fmt:message key="msg_valid" bundle="${message}" />
<%;%>
The error is this:
Syntax error on token "=", ; expected
As nicely stated in one of the post by Aniket Kulkarni:
JSTL variables are actually attributes, and by default are scoped at
the page context level. As a result, if you need to access a JSTL
variable value in a scriptlet, you can do so by calling the
getAttribute() method on the appropriately scoped object (usually
pageContext and request)
You can try this:
<%
String mesg= (String)pageContext.getAttribute("message");
out.println(resp);
%>
In PHP we can do the following with the help of Variable variables in PHP:
$privateVar = 'Hello!';
$publicVar = 'privateVar';
echo $$publicVar; // Hello!
Suppose we have the following chunk of Java code:
request.setAttribute("privateVar", "Hello!");
request.setAttribute("publicVar", "privateVar");
I've tried the following but an error occurs.
${${publicVar}}
Does anyone know how we can get value of privateVar via using only publicVar in JSP (JSTL)?
UPDATE 1:
I have a custom tag which allows to print a message if an object foo doesn't have a field bar.
I know I must catch exceptions in the case but I don't want to handle ones in JSP. I want to do it only in CustomTag file.
<%-- JSP file --%>
<ctf:tagName varName="foo.bar" />
<%-- CustomTag file --%>
<%# attribute name="varName" required="true" rtexprvalue="true"%>
<c:catch var="exception">
<c:set var="valX" value="${${varName}}" scope="page"/>
</c:catch>
<c:if test="${exception != null}">Can't find getter for the VAR in the OBJ.</c:if>
UPDATE 2:
JB Nizet gave me the answer and the following works well! :)
<c:set var="privateVar" value="Hello!" />
<c:set var="publicVar" value="privateVar" />
${pageScope[pageScope.publicVar]}
I don't think you can directly do this in the same way that you can in PHP. Instead you could change the attribute to use the value of the privateVar instead of the name, like this:
String privateVar = "Hello!";
request.setAttribute("privateVar", privateVar);
request.setAttribute("publicVar", privateVar);
This gives you access to the value under both names, which I think is the closest you'd get. No need to even put the attribute privateVar in the request if you are ultimately going to use publicVar on the JSP.
Ultimately you may want to rethink the design here as it doesn't really work in Java.
The basics:
That's not JSTL but Expression Language. And you should only use a single ${} evaluator. The code would be:
${publicVar}
More info:
StackOverflow Expression Language wiki
To your problem:
Expression Language doesn't allow that. You cannot have private attributes in any scope (page, request, session, application), so you can at most set the attribute twice with different names but the same value. But as you may note, this is useless.
<%for (String st : geocodePhoto.keySet()) {%>
alert(<%=st%>); // not work
alert(<%=geocodePhoto.get(st).getX()%>); // work fine
alert(<%=geocodePhoto.get(st).getY()%>); // work fine
alert(<%=geocodePhoto.get(st).getDate()%>); // not work
<%}%>
getX is return double value and getDate return String value like 'yy:mm:dd hh:mm:ss'
st has same form 'yy:mm:dd hh:mm:ss'
2,3 line alert is work fine but 1,4 line alert is doesn't work
what is wrong?
The <%= %> tag in JSP acts as if it calls String.valueOf() with the expression in the tag as the parameter, and writes the returned value to the output. So, your generated JavaScript source probably looks something like this:
alert(13:11:23 10:30:17);
alert(-0.06);
alert(51.5);
alert(13:11:23 10:30:17);
You're trying to pass text to the first and last calls to alert, but you aren't putting the text in quotes - so, you're getting a syntax error. The middle two calls are writing numbers into your JavaScript source - as a numeric constant is valid JavaScript, they work without being quoted.
So, your JSP code should look like this:
alert("<%=st%>");
alert(<%=geocodePhoto.get(st).getX()%>);
alert(<%=geocodePhoto.get(st).getY()%>);
alert("<%=geocodePhoto.get(st).getDate()%>");
Pass string through ""
alert("<%=st%>");
alert("<%=geocodePhoto.get(st).getDate()%>");