I am trying to pass String array from one jsp to another. I am using JSTL in my JSP.
In my first JSP i am doing like this
<c:if test="${fn:length(empBean.additionalEmailAddr) gt 0}">
<c:forEach begin="0" end="${fn:length(empBean.additionalEmailAddr) - 1}" var="ind" >
<input type="hidden" name="inbdAdditionalEmailAddr" value="${empBean.additionalEmailAddr[ind]}"/>
</c:forEach>
</c:if>
and trying to access the values in another jsp as follows
<%
String[] inbdAddEmlAddr = request.getParameter("inbdAdditionalEmailAddr");
%>
and i am planning to use JSTL to print the array values.
In the second jsp i am getting type mismatch error. Please help.
Is this the right approach ? Any help is appreciated
Thanks
request.getParameter() returns a String which the code attempts to assign to a String[], causing the exception.
Use request.getParameterValues('inbdAdditionalEmailAddr'); to retrieve parameters as an array.
See the documentation.
Related
For example, I want a field to show up if it's past 12 PM and the user role is teacher.
EDIT: I'm asking if there is a way to get the role of the user in JSP. What's the proper way to retrieve that?
Can I write it like this:
<c:if test ="${(Bean.after12PM) and (hasAnyRole('teacher'))}">
//some code
</c:if>
Is it the syntax for the jstl if or the value which is passed from a class or a object , not sure what your looking for
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
hope this helps
<c:set property="currentTime" value="${System.CurrentTimeMillis()}"/>
<c:set property="midnight" value="SET YOUR CUSTOM MIDNIGHT"/>
<c:set property="role" value="SET YOUR CUSTOM ROLE"/>
<c:if test="${currentTime > midnight && role=="teacher"}">
.....
</c:if>
Should be something like this, check for the correct "and" sintaxe inside that "test" it is been a while I can't recall it exactly.
Remember you can set any of those variables "midnight, role, currentTime" on your controller and set it on session or send it through request
I am pretty new in Spring MVC and JSP page and I have the following problem.
Into a controller class I add these 2 collections to my model object:
List<Integer> numeroProgettiWifiScuoleList = new ArrayList<>();
List<Integer> numeroProgettiPnsdScuoleList = new ArrayList<>();
by these lines:
model.addAttribute("numeroProgettiWifiScuoleList", numeroProgettiWifiScuoleList);
model.addAttribute("numeroProgettiPnsdScuoleList", numeroProgettiPnsdScuoleList);
And it works fine. The problem is that now into my JSP page I have something like this:
<c:forEach items="${listaScuoleDS}" var="scuola" varStatus="item">
<c:if test="${numeroProgettiWifiScuoleList[item.index] == 0}">
<p>Nessun progetto WIFI associato alla scuola</p>
</c:if>
</c:forEach>
So, as you can see, into the forEach cycle I have to perform an if test that check if the value of the numeroProgettiWifiScuoleList collection related to the current item in the iteration (item.index) is equal to 0. In this case show a text.
But in this way don't work (the tag is not shown).
Why? What am I missing? How can i fix this issue?
You have to give the var name in IF condition..Because you are using JSTL tag to get the value.
<c:set var ="numeroProgettiWifiScuoleList" value=${numeroProgettiWifiScuoleList}">
<c:forEach items="${listaScuoleDS}" var="scuola" varStatus="item">
<c:if test="${scuola[item.index] == 0}">
<p>Nessun progetto WIFI associato alla scuola</p>
</c:if>
</c:forEach>
I have the followin question, how will I pass multiple values from one jsp page to another? I have i piece of code here, which works fine, but it only sends one value from one page to another (year):
<form method="post" action="Display data.jsp" name="inputpage" >
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
</select>
For example if I had another value, for example
String str = "value";
Is it possible to send it using form post method as well? I googled it, and the answer I found included loops and too much code, is there short and simple way of doing it? Many thanks!
When you submit the form all values of the form will be passed, they only need to be inside the form. You can read other values normally by using:
request.getParameter(ParamName)
Take a look at this article for more information
You can send as many variable you want by Form Method.
For sending the value of String Str, assign its value to hidden field as:
<input type="hidden" id="hidden1" value=<c:out value="${variableName}" />
where variableName=str.
Could you use a hidden input inside your form to pass other data using the form post?
<input type='hidden' id='myExtraData' value='hello' />
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.
I want to understrand what happens when i use JSTL to access maps,in Hidden features of JSP/Servlet
in #blausC's answer, he explained what happend, but when i try to use the following code
<c:set var="resultMap" value="validationResults" scope="request"></c:set>
<c:if test="${resultMap['userName'] != null}">
${resultMap['userName'].details}
</c:if>
a confused exception happend
Caused by: javax.el.PropertyNotFoundException: Property 'userName' not found on type java.lang.String
The key of the map should be string, so whay is this exception, i tried the examples in the above question and the same exception,Can some one tell me where I have misunderstanding?
Edit: I populate the map in the servlet and send it to jsp
Map<String, ValidationResult> result = new HashMap<String, ValidationResult>();
aValidationResult = new ValidationResult();
check whether the field is valid or not if not fill the map
result.put("userName", aValidationResult);
result.put("group", aValidationResult);
if map is not empty, return the map to jsp
request.setAttribute("validationResults", result);
the map is filled when i make server side validation ,
Thanx in advance.
resultMap is a String because of this line
<c:set var="resultMap" value="validationResults" scope="request"></c:set>
You need to use EL to assign the value
<c:set var="resultMap" value="${validationResults}" scope="request"></c:set>
Edit: The following is working code
<c:set var="validationResults" value="<%= new java.util.HashMap() %>" />
<c:set target="${validationResults}" property="username" value="Hello World" />
<c:set var="resultMap" value="${validationResults}" />
<c:out value="${resultMap['username']}"></c:out>
This is caused because String class does not have a method called getUserName()