For example
<jsp:useBean id="total" class="java.util.LinkedHashMap"/>
// need somehow do something like this: total.put('key', 'value');
But without using scriptlets (it's obvious but a little bit ugly)
You can use JSTL <c:set> for this.
<jsp:useBean id="total" class="java.util.LinkedHashMap"/>
<c:set target="${total}" property="key" value="value" />
Related
if you wanna call the getter method from a specific bean using a dynamic key you use like that:
${bean[getterName]}
but if you wanna call double nested, or triple nested properties with a dynamic name how does it works, is it possible?
${bean.propertyA.propertyB} WORKS
${bean[propertyA.propertyB]} DOES NOT WORKS
<c:set var="dynamicKey" value="propertyA.propertyB" />
${bean[dynamicKey]} DOES NOT WORKS
UPDATE:
For now we're handling like that:
<c:forTokens items="${property}" delims="." var="item">
<c:set var="value" value="${value[item]}" />
</c:forTokens>
Dot notation vs brackets notation with nested properties:
${bean.propertyA.propertyB}
${bean[propertyA.propertyB]} ==> Not right, instead
${bean["propertyA"]["propertyB"]}
Your example with JSTL:
<c:set var="dynamicKey" value="${bean['propertyA']['propertyB']}" />
<c:out value="${dynamicKey}" />
After 2 years we are leaving like this since it does not seems to have a big impact on performance.
<c:forTokens items="${property}" delims="." var="item">
<c:set var="value" value="${value[item]}" />
</c:forTokens>
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 been googling and found that I can remove a session attribute using:
<c:remove var="foo" />
What I want is to clear all session attributes from a JSP, something like this:
<c:forEach var="item" items="${sessionScope}">
<c:remove var="${item }" scope="session"/>
</c:forEach>
The problem is that the code from above gives me this warning
c:remove doesn't support runtime expression
And I can't view the JSP where I put the code.
Is it possible? Is it a good practice to do something like this?
Because your are iterating through "item" and while iterating it inside loop you are trying to delete.
Better do this logic in java only rather than JSP
I'm looking to set the key value pairing of a HashMap using JSTL only. Is this possible?
I know how to retrieve the key value pairs, but I haven't found a way to set them.
Any help would be appreciated.
Example of retrieving HashMap key/value pairs using JSTL:
<c:forEach var="hash" items="${myHashMap}">
<c:out value="${hash.key}" />
<c:out value="${hash.value}" />
...
You can use the <c:set>.
<c:set target="${myHashMap}" property="key" value="value"/>
I wouldn't use JSTL to do that, but straight-up JSP will get it done...
<%
myHashMap.put("hello", "world");
%>
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.