Problem reading JSTL in the JSP page
Hello All,
I have two jsp files. One AccountStatus.jsp page where I defined the variables ReportStartMonth, ReportStartYear, ReportStartDay. I am initializing these values to default values for the first time users logs in. Then I would include one file called as SingleDateSelectBox.jsp page. Where I would read these variables. For some reason I am not able to read all three variables that I set in the AccountStatus.jsp. Please help me if you guys know where I am going wrong.
I am getting ReportStartMonth on the SingleDateSelectBox.jsp page, when I try to compare with using
<c:choose>
<c:when test="${ReportStartMonth == '1'}" >
<option value="1" selected>Jan
</c:when>
<c:otherwise>
<option value="1">Jan
</c:otherwise>
</c:choose>
always this goes to else part which is otherwise clause.
Can you please tell me what is going on here.
thanks..
AccountStatus.jsp code..
-
<%
// Get beginning and ending years
String ReportStartMonth = request.getParameter("ReportStartMonth");
if (ReportStartMonth == null) {
ReportStartMonth = "2";
out.println("<br>");
out.println("ReportStartMonth: " + ReportStartMonth);
out.println("<br>");
}
String ReportStartDay = request.getParameter("ReportStartDay");
if (ReportStartDay == null){
ReportStartDay = "2";
out.println("<br>");
out.println("ReportStartDay: " + ReportStartDay);
out.println("<br>");
}
String ReportStartYear = request.getParameter("ReportStartYear");
if (ReportStartYear == null){
out.println("<br>");
ReportStartYear = "2006";
out.println("ReportStartYear: " + ReportStartYear);
out.println("<br>");
}
String ReportEndMonth = request.getParameter("ReportEndMonth");
if (ReportEndMonth == null)
ReportEndMonth = "2";
String ReportEndDay = request.getParameter("ReportEndDay");
if (ReportEndDay == null)
ReportEndDay = "2";
String ReportEndYear = request.getParameter("ReportEndYear");
if (ReportEndYear == null)
ReportEndYear = "2007";
String param3 = ReportStartMonth + "/" + ReportStartDay + "/" + ReportStartYear;
out.println("param3 is: " + param3);
out.println("<br>");
//String param4 = request.getParameter("end_month") + "/" + request.getParameter("end_day") + "/" + request.getParameter("end_year");
//String param3 = request.getParameter("StartDate");
//if (param3 == null)
// param3 = "";
String param4 = request.getParameter("EndDate");
if (param4 == null)
param4 = "";
int param1 = 295;
//if (param1 == null)
// param1 = "";
%>
<jsp:setProperty name="GetAccountDetails" property="startDate" value="<%= param3 %>" />
<jsp:setProperty name="GetAccountDetails" property="endDate" value="<%= param4 %>" />
<jsp:setProperty name="GetAccountDetails" property="customerID"value="<%= param1 %>" />
<tr>
<td>Date range:
<%-- <input type="TEXT" name="OldStartDate" size="6" value="<%=//param3 %>"> --%>
<jsp:directive.include file="SingleDateSelectBox.jsp" />
SingleDateSelectBox.jsp Code
--
From :
<%
out.println("<br>");
out.println("ReportStartMonth in included file: " + ReportStartMonth);
out.println("<br>");
%>
<select name="ReportStartMonth" size="1">
<c:choose>
<c:when test="${ReportStartMonth == '1'}" >
<option value="1" selected>Jan
</c:when>
<c:otherwise>
<option value="1">Jan
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ i == 2}" >
<option value="2" selected>Feb
</c:when>
<c:otherwise>
<option value="2">Feb
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '3'}" >
<option value="3" selected>Mar
</c:when>
<c:otherwise>
<option value="3">Mar
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '4'}" >
<option value="4" selected>Apr
</c:when>
<c:otherwise>
<option value="4">Apr
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '5'}" >
<option value="5" selected>May
</c:when>
<c:otherwise>
<option value="5">May
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '6'}" >
<option value="6" selected>Jun
</c:when>
<c:otherwise>
<option value="6">Jun
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '7'}" >
<option value="7" selected>Jul
</c:when>
<c:otherwise>
<option value="7">Jul
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '8'}" >
<option value="8" selected>Aug
</c:when>
<c:otherwise>
<option value="8">Aug
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '9'}" >
<option value="9" selected>Sep
</c:when>
<c:otherwise>
<option value="9">Sep
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '10'}" >
<option value="10" selected>Oct
</c:when>
<c:otherwise>
<option value="10">Oct
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '11'}" >
<option value="11" selected>Nov
</c:when>
<c:otherwise>
<option value="11">Nov
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${ReportStartMonth == '12'}" >
<option value="12" selected>Dec
</c:when>
<c:otherwise>
<option value="12">Dec
</c:otherwise>
</c:choose>
</select>
--
[6796 byte] By [
lsd4600a] at [2007-11-26 17:30:11]

# 1
Sorry, that's a lot of code - but upon quickly scanning over the code I see a few things you could correct.
The HTML you have inside <c:when></c:when> and <c:otherwise></c:otherwise> is not well formed.
You have an open <option> tag but do not have a close </option> tag.
Try matching all the open <option> tags with the closed ones and see if it works.
To fix additional/possible HTML errors run your page through a HTML validator.
# 2
Thanks for the reply.
Close Option tag is optional, that is the reason I did not close it. The main problem I have is that, when the ReportStartMonth variable does have the value. <c:when does not able to recognize, all the time page goto the else part.
Can you please tell me where I am doing wrong?>
# 3
any idea guys.. Please let me know.
# 4
You said when ReportStartMonth does have a value.
What value does ReportStartMonth have? and what code is being executed?
The close </option> tag is not optional , where did you read that it is optional?
[Off Topic] If your HTML is well formed that means it conforms to XML welformedness syntax, then you will have fewer cross-browser compatibility problems. That means your code will work the same in all browsers. It will also eliminate other errors.
# 5
Hello,
I am setting the ReportStartMonth=2 as default if request.getParameter(ReportStartMonth) is null. Below is the code where I set the value. Please let me know where I am doing wrong.
String ReportStartMonth = request.getParameter("ReportStartMonth");
if (ReportStartMonth == null) {
ReportStartMonth = "2";
out.println("
");
out.println("ReportStartMonth: " + ReportStartMonth);
out.println("
");
}
# 6
Just before the begining of the <c:if statement , print the value of the variable ReportStartMonth , to verify that it does indeed have 2 in it. Because, if String ReportStartMonth = request.getParameter("ReportStartMonth"); is set to 1 then it will always show January.>
# 7
Ok, you seem to be making the same mistake that a LOT of people are making lately.
EL != java scriptlet.
You cannot just use a java scriptlet variable as an EL variable.
EL variables are the attributes in page, request, session and application scope.
They have no direct link to variables declared in scriptlet code.
Even if they have the same name, they are different variables.
${myName} is approximately equivalent to pageContext.findAttribute("myName}"/>
so given the following code
[code]<% String myName = "evnafets" %>
<c:out value="${myName}"/>
The result is NOT printing "evnafets", because pageContext.findAttribute("myName") will return null.
Next issue: code elegance.
I'm sorry, but I shudder at the code you have written here for printing the months of the year into a select box.
Yes it works, but it is repetitive copy/paste code with if/else conditions.
A simple loop would make it so much more compact.
JSP2.0 code:
<%
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
pageContext.setAttribute("monthList", months);
%>
<form>
<select name="ReportStartMonth" size="1">
<c:forEach var="month" items="${monthList}" varStatus="status">
<option value="${status.count}"${(param.ReportStartMonth == status.count) ? 'selected' : ''} >${month}
</c:forEach>
</select>
<input type="submit" value="go!">
</form>
# 8
Hello,
I did print it on the page, it does display as 2. here is the debug output look like.
ReportStartMonth in included file: 2
ReportStartDay in included file: 1
ReportStartYear in included file: 2000 .
I have no clue why the month is not set to Feb.
Thanks,
# 9
I agree with evnafets about making the code simpler.
Here's the problem:
If statements are executed sequentially.
When the code executes the first thing it encounters is the first IF statement which checks for January.
That first IF statement has an OTHERWISE statement, when
ReportStartMonth = 2 , the otherwise of the January IF statement executes.
That's whay no matter what is stored in ReportStartMonth , the otheriwse of the January IF statement will execute.
-
Another problem:
This code is below the January IF statement
<c:when test="${ i == 2}" >
<option value="2" selected>Feb
</c:when>
Instead of the above, shouldn't it be:
<c:when test="${ ReportStartMonth == '2'}" >
<option value="2" selected>Feb
</c:when>
Message was edited by:
appy77
# 10
Hello Evnafets,I am a newbie to EL. I thought of writing that down. I will try your code. Thanks for the explanation.
# 11
Hello Evnafets,
The code you told me, it works fine when I select the Month. In my case I would like to set the default month. If you look at my AccountStatus.jsp page. I am setting the ReportStartMonth=2. with your code, page is not defaulting to Feb. It is still displaying Jan. Below is the debug output.
ReportStartMonth in included file: 2
ReportStartDay in included file: 2
ReportStartYear in included file: 2006
Thanks,
lax..
# 12
>The code you told me, it works fine when I select the Month. In my case I would
> like to set the default month. If you look at my AccountStatus.jsp page. I am setting the ReportStartMonth=2. with your code, page is not defaulting to Feb. It is still displaying Jan. Below is the debug output.
The code I posted was just quick and dirty to use as an example.
However it shouldn't be hard to change it.
Right now it is getting the selected value from ${param.ReportStartMonth}
- ie the request parameter "ReportStartMonth"
It is easy enough to replace that with a JSTL variable, and then work out what the value should be:
<%
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
pageContext.setAttribute("monthList", months);
%>
// set default value
<c:set var="selectedMonth" value="2"/>
// set value from request parameter if present
<c:if test="${not empty param.ReportStartMonth}">
<c:set var="selectedMonth" value="${param.ReportStartMonth}"/>
</c:if>
<form>
<select name="ReportStartMonth" size="1">
<c:forEach var="month" items="${monthList}" varStatus="status">
<option value="${status.count}"${(selectedMonth == status.count) ? 'selected' : ''} >${month}
</c:forEach>
</select>
<input type="submit" value="go!">
</form>
Note that mixing Java scriptlet code and JSTL code is normally a bad idea.
I aim for 100% scriptlet free code in any JSP page I write.
The scriptlet code here is was for example only.
I would normally do it as a bean call, or servlet setting up an attribute.
Cheers,
evnafets
# 13
Thanks, it is working fine. I will try to create a bean and set the property using the bean. Thanks for your help.