Does any one know how to create Empty Array In JSTL (JSP Standard Tag Library) - jstl

I am trying to create an empty in my jstl page is it possible to create an empty array and assign some value to that array in the jstl page itself .?

Your question is quite unusual. You want to do scripting without a scriplet. The only way that I could think of is to use the useBean tag to create an ArrayList. Its add method returns a boolean. That is why I used empty if tags to insert the elements. The forEach tag is not necessary. I used it to display the elements.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myList" class="java.util.ArrayList" />
<c:if test = '${myList.add("My first element")}'>
</c:if>
<c:if test = '${myList.add("My second element")}'>
</c:if>
<c:forEach var="element" items="${myList}">
${element}
</c:forEach>
Output:
My first element My second element

You can create an array with some values using c:set:
<c:set var="array" value="${['item a','item b','item c']}" />
<c:forEach var="item" items="${array}">
<c:out value="${item}" /><br />
</c:forEach>

Related

How to get values for dynamic form field names using JSTL and EL

I have 8 checkboxes and 1 button, when the user check any of the checkboxes and click the button, I want to check if any of the checkbox is checked and display it in another .jsp
I have already referred to few similar questions with no luck so far. So i tried to manage with my own logic
First.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<input type="checkbox" id="seat" name="seat${loop.index}" value="seat${loop.index}" >
<label for="seat">Seat${loop.index}</label>
</c:forEach> <br> <br>
<input type="submit" value="Save" name="savebtn">
Second.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<c:if test="${not empty param.seat[loop.index]}">
<c:out value="${param.seat1} is booked"/>
</c:of>
</c:forEach>
I have 2 problems regarding the code above :
i can't get loop.index value inside the param $param.seat[loop.index] doesn't work
And even if i try to do it manually, i can only get value from seat1. I can't get value from the rest ( seat2, seat3, etc).
${param.seat[loop.index]} implies that seat is a collection, which it is not (it probably does not even exist). You are after ${param.seatX}, where you can dynamically set X. You can do that by creating a variable containing the parameter name first:
<c:set var="seatVarName" value="seat${loop.index}"/>
Now you can use this variable to get the parameter value from the implicit EL object:
${param[seatVarName]}
See also:
JSP expression language and dynamic attribute names

Output not visible jstl c:out not showing on browser

Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

How to get index in jstl display table

I have display table like below
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
</display:table>
Now I want to get index of loop like for first element 0 for second 1 and 2,3,4... go on. How it is possible with display table.
For just reference we can do like this in JSTL forEach loop with help of varStatus variable like below
<c:forEach items="${sample}" var="clm" varStatus="status">
${status.index}
</c:forEach>
So is this possible with display table?
display table tag implicitly exposes row number named id_rowNum where id is specified in display table tag.
In your scenario:
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
<display:column title="Row Number" >
<c:out value="${sample_rowNum - 1}"/>
</display:column>
...
</display:table>
Also make sure to include core tag as:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
You can find more information about implicit objects of display table tag here.

How can I display error in JSP without a <form:form>

I have a jsp page with a List<Object> as the #ModelAttribute. However, there are no <form:form> tags in the page. All I'm doing is print the contents of the List.
In my Controller.java, I'm binding an error by doing:
result.rejectValue("", "NOT_LOGGED_IN", "You should Login first") ;
But since I dont have a form in my jsp, I'm not able to access the error with:
<form:errors path="" /> <br/>
Please tell me how to access the error (or what I'm doing wrong).
In your controller:
model.addAttribute("errors", result.getAllErrors());
In your JSP:
<c:forEach items="${errors}" var="error">
<%-- do want you want with ${error} --%>
<c:out value="${error.defaultMessage}" />
</c:forEach>
Associate global errors this way:
result.reject("NOT_LOGGED_IN", "You should Login first") ;
You can show the global errors in the jsp :
<form:errors cssClass="error" delimiter="<p/>" />
for any specific error
set in your code like-
model.addObject("errorMsg","username/password failed");
And show this error on jsp in this way:
<c:out value="${errorMsg}"/>
This way you would get your error on jsp.
there's a way to reference the error like
instead of
<form:form commandName="object">
<form:errors path="field"/>
</form:form>
best
Check if Expression Language evaluation is enabled in JSP(By default it is disabled).If not add below code to enable it.
<%# page isELIgnored="false" %>

Spring JSP Select List for Range of Numbers

I'm creating a time entry system where the user has the option of selecting the hour and minute via two separate drop down select boxes. So the hour box has the numbers 1-12 in it, and the minute box has 00-59.
This is part of a Spring 2.5 Java EE project.
I have this in my JSP, for example, to create the option values as part of a select dropdown list:
<% for( int i=1; i<=12; i++) { %>
<option value="<%=i %>" <%= Integer.parseInt(time1fromHr)==i?selected:"" %> />
<% } %>
The for loop generates all the hours and marks the currently selected hour as default. Well, it looks pretty ugly to me, mostly because there is quite a bit of Java code involved here and I was wondering if there is a more elegant solution to approaching this problem using JSP tags or the Spring library. I am passing in the currently set parameters via the ModelAndView object in Spring.
In your model you could pass a List of Integers for hours, and another for minutes. Then you could use the form:select tag.
<form:select path="hour">
<form:options items="${hours} />
</form:select>
If your command object for the form has the selected value set in the "hour" value, and the model contains 1-12 in the "hours" value, then it should render the select and take care of marking the appropriate option selected. Then you do the same for minutes.
If you don't want to go the spring form taglib direction, you could again place the hours in the model and use JSTL. Something like:
<c:forEach var="hour" items="${hours}">
<c:if test="${hour} == ${selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour} != ${selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>
I know there's a better way to do the c:if part, maybe using a c:choose, but you get the gist. You have your selected value in selectedHour and your choices in hours in the model.
Yes there is, this part of Spring MVC:
#RequestMapping(value="/index.html",method=RequestMethod.GET)
public String form(ModelMap map) {
Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("CHINA", "China");
country.put("SG", "Singapore");
country.put("MY", "Malaysia");
map.addAttribute("countryList", country);
return "index";
}
and then use:
<form:select path="country" items="${countryList}" />
don't forget to add Spring type Library to your page:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Use JSTL for comparison
<c:forEach var="hour" items="${hours}">
<c:if test="${hour == selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour != selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>
OR
<c:forEach var="hour" items="${hours}">
<c:if test="${hour eq selectedHour}">
<option value="${hour}" selected="selected">${hour}</option>
</c:if>
<c:if test="${hour ne selectedHour}">
<option value="${hour}" >${hour}</option>
</c:if>
</c:forEach>

Resources