How should i convert the scriptlet to JSTL? - java

<A HREF='<%=urlProfile%>'><%=objUserDetailsVO.getLogin_Ident()%></TD>
<%if(objUserDetailsVO.getFlgBifNonBif().trim().equals("Y")){
nonBifFlag="*";
}
%>

You need to set objUserDetailsVO in request/session/page/application context that is available to this jsp. (choose the scope that is most suitable here),
Now
<A HREF='${urlProfile}'${objUserDetailsVO.login_Ident}'/></TD>
<c:if test="${objUserDetailsVO.flgBifNonBif=='Y'>
<c:set var="nonBifFlag" value="*"/>
</c:if>
Note:
You need urlProfile & objUserDetailsVO available to JSP as mentioned above.
Update: (on your new Question)
<c:if test="${empty NONBIFUPDATEMODE}">
NONBIFUPDATEMODE is empty or null.
</c:if>

Related

Is there a better way to write this JSP custom tag?

I'm creating a JSP .tag file that will handle this use case:
<my:safeParam paramName="param1" defaultValue="testvalue"/>
Where the behavior will be to take a request parameter, escape its value for "safe" usage, and place that escaped value back on some scope (e.g. request) under the same name as the parameter (although it could be another name).
I have an implementation that works, but I've got scriptlet in there because I couldn't find a way to use variable variable names in just JSTL. But I'm no JSTL wizard, so I thought I'd see if there's a syntax/approach I'm missing. Here's the working safeParam.tag file:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%# attribute name="paramName" required="true" %>
<%# attribute name="defaultValue" %>
<%
String name = (String) pageContext.getAttribute("paramName");
%>
<c:if test="${not empty defaultValue}">
<%
request.setAttribute(name, pageContext.getAttribute("defaultValue"));
%>
</c:if>
<c:if test="${not empty param[paramName]}">
<c:set var="escaped" value="${fn:escapeXml(param[paramName])}"/>
<%
request.setAttribute(name, pageContext.getAttribute("escaped"));
%>
</c:if>
(I sure wish EL was escaped automatically.)
<c:if test="${empty paramName}">
${defaultValue}
</c:if>
<c:if test="${not empty paramName}">
<c:out value="${paramName}" escapeXml="true"/>
</c:if>
I probably won't use this approach because it reduces the conciseness I was seeking with this custom tag, but just to document it as an option... (I'm not sure if this is what Frank Yeung is getting at.)
I could make the tag simply output the default-or-escaped parameter value, then make the user of the tag wrap that in a <c:set>.
Tag:
<c:choose>
<c:when test="${empty param[paramName]}">
${defaultValue}
</c:when>
<c:otherwise>
<c:out value="${param[paramName]}"/>
</c:otherwise>
</c:choose>
JSP:
<c:set var="myVariable">
<my:safeParam paramName="folder" defaultValue="homeFolder"/>
</c:set>
But really my goal has been to do everything inside the tag.

loops binary tree in jstl

I create a binary tree and now I would like to display the result in my jsp with conditions.
Each node has 4 elements (id, question, answer, and leftnode rightnode)
If the user clicks "Yes", the program goes to the left of the tree and if he answers "no" to the right of the tree.
For example, the initial question is "you're a man?" If he answers "yes" we will go to the left and page view "you're a singer?" If he answers "no" page displays "you're French? ".
In java, it would to the original question.
cursor [i]. GetQuestion ()
Then For yes
cursor [i] getQuestion. GetLeftnode ()
and for no
cursor [i]. GetRightnode (). GetQuestion
Until the, everything works normally, but when I want to loop until there is no longer any issue by
<c:when test="${not empty cursor[i].getQuestion() }">
the program stops at the first loop and stops it that cursor is not empty
here is my complete code jstl
<c:choose>
<c:when test="${not empty cursor[i].getQuestion() }">
<c:if test="${param.btn eq 'Oui'}" var="oui">
<c:set var="cursor" value="${cursor[i].getLeftnode() }" scope="session"></c:set>
<c:set var="i" value="${i+1 }" scope="session"></c:set>
</c:if>
<c:if test="${param.btn eq 'Non'}" var="non">
<c:set var="cursor" value="${cursor[i].getRightnode() }" scope="session"></c:set>
<c:set var="i" value="${i+1 }" scope="session"></c:set>
</c:if>
</c:when>
</c:choose>
<c:out value="${cursor.getQuestion() }"></c:out>
thank you
Rather than using methods like ${cursor.getQuestion() } you have to use property names like this: ${cursor.question }
P.S. And make sure you have a proper getter names (get should be lower case)
Thank you for your help.
I found the solution. For those who are interested, here is the code that works
<c:set var="cursor" value="${nodes }" scope="application"></c:set> <form method="get">
<c:choose>
<c:when test="${not empty cursor.getQuestion() }">
<c:if test="${param.btn eq 'yes'}" var="yes">
<c:set var="cursor" value="${cursor.getLeftnode() }" scope="session"></c:set>
</c:if>
<c:if test="${param.btn eq 'no'}" var="no">
<c:set var="cursor" value="${cursor.getRightnode() }" scope="session"></c:set>
</c:if>
</c:when>
</c:choose>
<c:out value="${cursor.getQuestion() }"></c:out>
The problem was scope. At each loop, with "session" scoped my variable was reset each time. By "application" in scope, it is initialized only once and I can get what I want
Thank

if taglib comparing with two items of forEach jstl

I have an forEach taglib inside another one. I would like to compare one attribute of each object being listed in its respective taglib.
<c:forEach items="${andares}" var="andar">
..........
<c:forEach items="${lojas}" var="loja">
<c:if test="${loja.andar == andar.numero}">
.....
</c:if>
</c:forEach>
</c:forEach>
I would like to only execute some code when the attribute "numero" of object andar is equals to attribute "andar" of object loja.
How can I do it?
I don't recall, but it seems like that should work. If not, perhaps this?
<c:forEach items="${andares}" var="andar">
<c:set var="andarval" value="${anar.numero}"/>
..........
<c:forEach items="${lojas}" var="loja">
<c:set var="lojaval" value="${loja.andar}"/>
<c:if test="${lojaval == andarval}">
....
</c:if>
</c:forEach>
</c:forEach>

java error in jsp/jstl page

I have the following code:
<c:choose>
<c:when test="${empty sessionScope.languageRB}">
<html:hidden property="language" value="en"/>
<html:hidden property="country" value="GB"/>
</c:when>
<c:otherwise test="${not empty sessionScope.languageRB}">
<html:hidden property="language" value="<%=languageRB.getString("style.language")%>"/>
<html:hidden property="country" value="<%=languageRB.getString("style.country")%>"/>
</c:otherwise>
</c:choose>
languageRB is an attribute stored in session, of type ResourceBundle.
I want to do the following: if languageRB exists in session then the property is defined using the value of the string in the paranthesis, otherwise the property is set to a default value.
I'm getting the following error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 89 in the jsp file: /pages/common002-preparelogin.jsp
languageRB cannot be resolved
88: <c:otherwise test="${not empty sessionScope.languageRB}">
89: <html:hidden property="language" value="<%=languageRB.getString("style.language")%>"/>
90: <html:hidden property="country" value="<%=languageRB.getString("style.country")%>"/>
Firstly, you shouldn't mix scriptlets and taglibs/EL. Use the one or the other. As scriptlets are officially discouraged since a decade, you should forget about them and stick to taglibs/EL. Your concrete problem is caused because scriptltets are always invoked regardless of outcome of JSTL taglibs. They do not run in sync with taglibs based on the coding. You can visualize it as follows: scriptlets run form top to bottom first and then it's taglibs/EL's turn to run from top to bottom again. You should use EL to access the resource bundle property. Additional advantage is that EL is null-safe, it won't throw a NPE, but just bypass the property access.
Secondly, you've a new problem when you replace the scriptlet by EL, the <c:otherwise> doesn't support a test attribute at all. Get rid of it. It's already only hit when none of the <c:when> conditions have matched.
So, all with all, this should do:
<c:choose>
<c:when test="${empty sessionScope.languageRB}">
<html:hidden property="language" value="en"/>
<html:hidden property="country" value="GB"/>
</c:when>
<c:otherwise>
<html:hidden property="language" value="${languageRB['style.language']}"/>
<html:hidden property="country" value="${languageRB['style.country']}"/>
</c:otherwise>
</c:choose>
In expression you need to get your bundle from session directly:
<%=((ResourceBundle)session.getAttribute("languageRB")).getString("style.language")%>

How can I make JSTL give an error when referencing an undefined attribute

Inside a jsp page, I would like JSTL to behave strictly when referencing an undefined variable.
Example:
The servlet passes:
request.setAttribute("firstName", "hello");
request.setAttribute("lastName", "there");
The jsp page:
${firstName} ${middleName} ${lastName}
I would like JSTL to give me an error that middleName is undefined instead of silently ignoring it.
<c:if test="${empty middleName}">
<c:out value="Middle name is empty"/>
</c:if>
OR
<c:choose>
<c:when test="${empty middleName}">
<c:out value="Middle name is empty"/>
</c:when>
<c:otherwise>
<c:out value="Middle name is NOT empty"/>
</c:otherwise>
</c:choose>

Categories