How do I check that a parameter exists in JSTL? - jstl

I want to check a parameter is defined or not in JSTL irrespective of it has a value. For example,
I want to check parameter searchTerm exits. It's value may be empty. But how to check whether it exits?

<c:if test="${param.searchTerm != null}">
...
</c:if>

Related

Difference betwwen assign value in c:set value attribute and body attribute

Right now i am facing one serious problem. When i write following code
<c:set var="columnName" value="${columnObj.columnId}"/>
<c:out value="${columnRowObj[columnName]}"/>
i am not able to get output. here value of columnObj.columnId is 6856 and columnRowObj is resultset mapping variable.
Same above code when i write using body attribute
<c:set var="columnName">${columnObj.columnId}</c:set>
<c:out value="${columnRowObj[columnName]}"/>
i am getting output.
Anybody can please solve my problem what happens when i assign value in value attributes and when i assign it in body attribute.

Spring JSP variable not assigned

I have in my JSP page code like this:
<spring:url value="" var="url"/>
EN
And issue is that parameter url in link is always set to empty String.I would expect that if I type url like localhost:8080/test the url variable will hold this value and it will be replaced in link so it would look like /change_locale?locale=EN&current=test. However it is always generated like /change_locale?locale=EN&current=.What I am doing wrong? Best regards
In
<spring:url value="" var="url"/>
Your value value is the empty String. Because of this, the URL is relative.
Spring uses UrlTag to construct the value from a <url> tag. You'll want to take a look at its createUrl method in the source code if you're curious.
In this case, it will generate a value that is the empty String and store it in a page scope attribute named url. That's what you get when rendering
${url}

How to put conditional statements in .jsp pages

I need to have a conditional statement added on a .jsp page that identifies all authenticated pages in order to insert a specific tracking code in place. I am not accustomed writing any jstl but I have this so far
<c:if test='${not empty authenticatedUser}'>
my tagging code
</c:if>
I would like to an extra condition in place that exclude a specific url
Use this -
<c:if test="${not empty authenticatedUser && url ne 'someUrl.html'}">
my tagging code
</c:if>
Single quotes is coming two times. That might be creating an issue.
EL expressions (the ${not empty authenticatedUser}) can contain logical expressions like:
<c:if test='${not empty authenticatedUser and url ne "someUrl.html"}'>
my tagging code
</c:if>

Cannot access variable inside the included JSP

I am using Spring 2.5 in my application. In the view I have main jsp in which I have included another jsp. I have declared a variable using c:set tag in main jsp which I can not access inside the jsp. Below is code
main.jsp
<c:set var="hPediquestStudy"><spring:message code="study.hpediquest.mapping" /></c:set>
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
<html>
<head>
</head>
<body>
<c:if test="${currentStudy eq hPediquestStudy}">
Variables are equal
</c:if>
<c:if test="${currentStudy ne hPediquestStudy}">
Variables are not equal
</c:if>
<jsp:include page="/WEB-INF/jsp/included.jsp"></jsp:include>
</body
</html>
included.jsp
<c:if test="${currentStudy eq hPediquestStudy}">
hPediquestStudy Variable is accessible
</c:if>
<br/>currentStudy : ${currentStudy}
<br/>hPediquestStudy : ${hPediquestStudy}
I am getting output
Variables are equal
currentStudy : hPediquest
hPediquestStudy :
Why on main jsp both values are equal while in included jsp I dont see its value?
Why currentStudy displays its value inside the included jsp?
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
I can see the value of hPediquestStudy value if I set that variable in included jsp like in main jsp. But I dont want to set it every time when I include the jsp. Please help
Why on main jsp both values are equal while in included jsp I dont see its value?
Because <c:set> by default stores them in the page scope.
Why currentStudy displays its value inside the included jsp?
Because it's available as session attribute as well.
is there any solution which helps me to access the variable set in the parent jsp ans can be accessed in the included jsps?
You need to set the scope attribute of <c:set> to request or above. The (default) page scope is exposed to current JSP only, not any included JSPs.
Note that the line
<c:set var="currentStudy" value='<%=(String)session.getAttribute("currentStudy")%>'/>
is unnecessary. The ${currentStudy} will already scan for variables in page, request, session and application scopes. As you've apparently already set it in the session scope, you don't need to copy it into the page scope. So, just remove that line. All with all, your top 2 <c:set> lines should be replaced with this single line:
<c:set var="hPediquestStudy" scope="request"><spring:message code="study.hpediquest.mapping" /></c:set>
and then it'll work the way you intended.
See also:
Our EL wiki page

Struts2 ognl execute request method

I'm trying to use OGNL to evaluate if the session is valid, in order to show some information. For that I've the following JSP
[...]
<s:if test="request.isRequestedSessionValid()">
[...] (show user name, etc)
</s:if>
But it doesn't work. I've also tried "#request.isRequestedSessionValid()","%{request.isRequestedSessionValid()}" and "{request.isRequestedSessionValid()}", but I always get an error message target java.lang.NullPointerException: target is null for method isRequestedSessionValid or [OgnlValueStack] Could not find method [#request.isRequestedSessionValid()]. What am I doing wrong?
Thanks!
Okay, now that I've had a chance to look deeper into this I am tossing out my old answer. #request won't work because that is basically the equivalent of the JSP EL expression ${requestScope.requestedSessionIdValid}, but that property isn't a request scope property, its an actual property of the HttpServletRequest.
You can do this easily with JSP EL using:
<c:if test="${pageContext.request.requestedSessionIdValid}">
...
</c:if>
I don't know that there is an equally concise way to get at this with OGNL.

Resources