Suppose I have the following code:
<c:when test="${isFoo}">
Where can isFoo be defined except:
java classes
<c:set ... /> construction ?
You can also set it using c:set. For instance:
<c:set var="isFoo" value="true" scope="page" />
In JSP itself by scriptlets. E.g. as a request attribute:
<% request.setAttribute("isFoo", true); %>
Scriptlets are however discouraged since over a decade.
By an implicit EL variable. E.g. as a request parameter:
http://example.com/page.jsp?isFoo=true
Which can be accessed by ${param}:
<c:if test="${param.isFoo}">
There are many more, you can find them all in this overview.
See also:
Our EL (Expression Language) tag wiki page
Related
what is a proper way to use variables from scriptlets in jstl?
I don't know what is wrong in my code:
<%
boolean a = true;
boolean b = false;
%>
<c:choose>
<c:when test="${a}">
<c:set var="x" value="It's true"/>
</c:when>
<c:when test="${b}">
<c:set var="x" value="It's false"/>
</c:when>
</c:choose>
It looks like it doesn't go into the whole block.
Variables in scriptlets cannot be seen in JSTL because Expression Language, the stuff between ${} used in JSTL, will look for attributes in page, request, session or application. You have to at least store the variable from scriptlet in one of these scopes, then use it.
This is an example:
<%
boolean a = true;
request.setAttribute("a", a);
%>
<c:if test="${a}">
<c:out value="a was found and it's true." />
</c:if>
More info:
Expression Language StackOverflow wiki
JSTL StackOverflow wiki
As a recommendation, stop using scriptlets. Move the business logic in your JSP to controller and the view logic into EL, JSTL and other tags like <display>. More info: How to avoid Java code in JSP files?
The default scope of JSP is page. if you want to use the variable of scriplet to JSTL use following code.
<%
boolean a = true;
boolean b = false;
pageContext.setAttribute("a", a);
pageContext.setAttribute("b", b);
%>
Then it will be usable in JSTL.
I have the following code:
<bean:define id="hasDocuments" name="BudgetSimulationDetailForm" property="hasDocuments" type="java.lang.Boolean"/>
<%
request.setAttribute("enablebtnRelatedDocs", "true");
request.setAttribute("hasDocuments", String.valueOf(hasDocuments));
%>
I want to remove the scriptlet, I tried using c:set with different scopes but it didn't work.
Is it possible to set a request attribute using JSTL tags?
I tried this and did not work:
<c:set name="enablebtnRelatedDocs" value="true" scope="request"/>
and also
<c:set name="enablebtnRelatedDocs" value="${true}" scope="request"/>
Afterwards there is an include:
<jsp:include page="/gema/jsp/includes/detail/top_Detail.jsp">
<jsp:param name="title_key" value="${title}" />
<jsp:param name="title_bundle" value="buc" />
<jsp:param name="standard_buttons_include" value="true" />
<jsp:param name="typeId" value="53555" />
<jsp:param name="detail" value="budget" />
</jsp:include>
Inside the included JSP the request attribute is not visible, apparently.
Sounds Good, You want to use JSP Standard Tag Library instead of Scriplet.
Yes, It's possible using c:set. Read more about Core Tag Library
<c:set var="enablebtnRelatedDocs" value="${true}" scope="request"/>
<c:out value="${requestScope.enablebtnRelatedDocs }"/>
By default c:set set a attribute in page context. you can set it in any scope.
By default, the JSTL Core library function "set" accepts the following attributes:
JSTL Core set property (credits to tutorialspoint.com) :
value,
target,
property,
var,
scope
You should use "var=" instead of "name=". Hope this helps!
Happy coding!
1:
I am beginner in java and trying to figure out how to send parameter from jsp to servlet using this
inside my jsp -
<%! String option;%>
<% option = request.getParameter("option"); %>
<jsp:useBean id="processBean" scope="session" class="helpers.ProcessBean" />
${processBean.processRequest(option)}
I want to send 'option' to processRequest but this way it is always empty string, so I am doing something wrong.Could not find anything online.
Any help is highly appreciated.
Variable declared/set on the page's scripting language is not automatically available from EL. You can manually make it available via:
<c:set var="option" value="<%= option %>" />
before using it in EL.
I have a JSTL loop where I'm trying to check to see if a given variable is empty or not with a dynamic variable name. When I use c:set with page scope, the variable is not accessible to the if statement. However, when I set it using <% pageCotnext.setAttribute(...); %>, the variable is available.
<%
pageContext.setAttribute("alphaParA", "test");
pageContext.setAttribute("alphaParF", "test");
int i = 0;
%>
<ul class="alphadex_links">
<c:forEach var="i" begin="0" end="25" step="1" varStatus="status">
<c:set var="currentLetter" scope="page">&#${i+65}</c:set>
<c:set var="currentPar" scope="page">alphaPar${currentLetter}</c:set>
<% pageContext.setAttribute("currentPar", "alphaPar" + (char)('A' + i++)); %>
<li>
<c:choose>
<c:when test="${not empty pageScope[currentPar]}">
The test is always fails when I remove the pageContext.setAttribute block, however it succeeds for A and F as it should when the block is in. I'm very confused and can't find help anywhere.
It fails because HTML doesn't run at the moment JSTL runs. You're effectively passing a Java String A to it instead of the desired character A which would be represented as such based on the HTML entity A when the HTML is retrieved and parsed by the webbrowser after Java/JSP/JSTL has done its job. Please note that your HTML entity is missing the closing semicolon, but this isn't the cause of your concrete problem.
As to the concrete functional requirement, sorry, you're out of luck with EL. It doesn't support char. Your best bet is to deal with strings like this:
<c:forEach items="${fn:split('A,B,C,D,E,F,G,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', ',')}" var="currentLetter">
<c:set var="currentPar" value="alphaPar${currentLetter}" />
${pageScope[currentPar]}
</c:forEach>
If necessary, just autogenerate the letters as String[] in Java end and set it as application attribute.
The c:if test always fails for me and it never gets inside the loop. I am using the following namespaces
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:c="http://java.sun.com/jstl/core"
The string ('array') to be split is "Tom and Jerry are GAP1 friends"
<s:decorate template="/layout/display-text.xhtml">
<c:set var="array" value="#{_mybean.value}"/>
<c:set var="space" value="#{fn:split(array, ' ')}"/>
<c:set var="len" value="#{fn:length(space)}"/>
<h:outputText value="total length = #{len}"/><br/>
<c:forEach begin="0" end="5" var="index">
<h:outputText value="index = #{index}, value = #{space[index]}"/><br/>
<c:set var="val" value="#{space[index]}"/>
<c:if test="#{fn:startsWith(val, 'GAP')}">
<h:outputText value="Found keyword parameter GAP" /><br/>
</c:if>
</c:forEach>
</s:decorate>
The JSTL core URI is invalid. As per the JSTL TLD it should be (note the extra /jsp):
xmlns:c="http://java.sun.com/jsp/jstl/core"
That said, mixing JSF with JSTL is never been a good idea. It won't always give results as you'd expect because they doesn't run in sync as you would expect from the coding. It's more that JSP/JSTL runs from top to bottom first and then hands over the produced result to JSF to process further from top to bottom again. That would cause some specific constructs to fail. Better use pure JSF components/attributes instead.
Instead of c:forEach, rather use Seam's a4j:repeat or Facelets' ui:repeat and instead of c:if make use of the rendered attribute of the JSF component which has to be toggled to show/hide. Instead of all that JSTL c:set, write appropriate code logic in managed bean constructor or action method or getter.
The JSTL functions (fn) taglib is however still highly valuable in JSF. You can keep using it.