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")%>
Related
I have Jstl coding in JSP Page as
<td style="${r.p46_readback-row.p46_readback eq 0 ? 'background-color: lime':'background-color: pink'}">
<fmt:formatNumber value="${(r.p46_readback-row.p46_readback)}" maxFractionDigits="2" minIntegerDigits="2" pattern="##.## (" var="nn"></fmt:formatNumber>
<c:out value="${nn}"></c:out>
<c:choose>
<c:when test="${r.p46_readback-row.p46_readback ne 0}">
<fmt:formatNumber value="${(r.p46_readback-row.p46_readback)/r.p46_readback}" maxFractionDigits="2" minIntegerDigits="2" type="percent" var="mm"></fmt:formatNumber>
<c:out value="${mm}" ></c:out><c:out value=")"></c:out></c:when>
<c:otherwise>
<c:out value="00%)"></c:out>
</c:otherwise></c:choose></td>
Now I need to pass variable nn into java bean as a argument to a method.How to do that.
Is it possible to pass variables set in tag into bean class??
There's a solution to help you archive this. Let me start from line 3 of your code:
<c:out value="${nn}"></c:out>
<%
Number number = (Number) pageContext.getAttribute("nn");
// create your java bean here and set the number variable to the bean
// after that you can do whatever you want with your bean
%>
When writing XML-compliant JSP, it is difficult to generate different HTML tags according to the input (e.g. when outputting the different tags in a <table>).
The standard solution is to use if or choose tags, but sharing code inside the HTML tag becomes a problem. E.g.
<c:choose>
<c:when test="${i.tag = 'th'}><th> <!-- 100 lines of code --> </th></c:when>
<c:when test="${i.tag = 'td'}><td> <!-- 100 lines of code duplicated?! --> </td></c:when>
</c:choose>
XSLT offers an <xsl:element> tag which allows you to build a tag and its attributes with standard XML syntax. Is there such a tag in any tag library in JSP?
Shouldn't you be doing this like?
<c:choose>
<c:when test="${i.tag = 'th'}"><th></c:when>
<c:when test="${i.tag = 'td'}"><td></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"></th></c:when>
<c:when test="${i.tag = 'td'}"></td></c:when>
</c:choose>
If you want your markup to be valid XML; make use of a custom tag that wraps all the Java code you want to avoid duplicating. Your markup would then look something like
<c:choose>
<c:when test="${i.tag = 'th'}">
<th>
<my:customTag anyAttributes="th-related-values-if-any" ... />
</th>
</c:when>
<c:when test="${i.tag = 'td'}">
<td>
<my:customTag anyAttributes="td-related-values-if-any" ... />
</td>
</c:when>
</c:choose>
Alternatively, the following hack (suggested by #Uooo) can be used to pass XML validators.
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="<th>%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="<td>%" /></c:when>
</c:choose>
<!-- 100 lines of code -->
<c:choose>
<c:when test="${i.tag = 'th'}"><c:out value="</th>%" /></c:when>
<c:when test="${i.tag = 'td'}"><c:out value="</td>%" /></c:when>
</c:choose>
You could create a seperate JSP containing your "100 lines of code" and include it.
Replace your <!-- 100 lines of code --> with:
<jsp:include page="hundredLines.jsp">
<jsp:param name="beanParam" value="beanValue"/>
</jsp:include>
And in your hundredLines.jsp:
Bean can be used like ${beanParam}
No. JSP requires knowing the tags when it builds its trees. That makes the operation of JSP more robust
However, you can address the duplicated code by factoring it into a separate JSP file and including it, depending on the implementation of JSP you are using.
Alternatively, you can create a JSP tag that holds the 100 lines of code. It is probably not worth while to do this.
Finally, you could, instead of using th and td, use styles on td to make td look like th. That would make the element look like
<td style="${i.tag == 'th' ? '' : 'font-style: bold; text-align: center;'}">
100 lines of code
</td>
I know I don't have the style quite right, but you could use styleClass instead and play with it in your CSS.
I am using the following code to compare two variables, but it seems it has an error as the page does not get loaded at all.
Based on this answer my code should be correct but surprisingly does not work.
If I remove the <c:when> line it shows
the values of ${name} and ${myvar.employee.name}.
<c:forEach var="myvar" items="${user.names}">
${name}
${myvar.employee.name}
<c:when test="${name eq myvar.employee.name}">
hello
</c:when>
The structure with <c:when> requires <c:choose>
<c:choose>
<c:when test="${your condition here}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
otherwise you will get error.
As Alex has pointed out, you cannot have non-wrapped <c:when> in your code.
But it looks like, in your case, <c:choose> is not required, because you have only a single clause. In this case <c:if> may be a better option:
<c:if test="${name eq myvar.employee.name}">
hello
</c:if>
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>
<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>