Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jstl c:if condition is not working

Posted on 2015-02-06
4
Medium Priority
?
568 Views
Last Modified: 2015-02-06
How program should work :
1) Once the user selects the file from first select box, second select box gets populated.
2) After selecting one option from second select box "hi there" must be displayed.

How the program is working
From above mentioned steps, once the user selects file from first select box the second select box is getting populated and at the same time before selecting any option from second select box "hi there" is displayed.
i want to display "hi there" after selecting an option from second select box.

I think it has something to do with : <c:if test='<%=cubeValue != "select" %>'> line of code.

<jsp:useBean id="c1" class="mypkg.Cubes"></jsp:useBean>

<div id="file">
<form name="s_file" method="post" action="">
<select id="selected_file" name="selected_file" onchange="s_file.submit();">
<option value="Select">Select schema</option>
<c-rt:forEach var="file" items="<%=fileNames%>">
 <option value="<c:out value="${file}"/>"><c:out value="${file}"/></option>
</c-rt:forEach>
</select>

<c:if test='${pageContext.request.method=="POST"}'>
<c:if test='${request.getParameter("selected_file") ne "Select"}' >

<c:set value="<%=fileValue %>" target="${c1}" property="schemaFile"/>
<c:out value="${c1.Connect()}"></c:out>


 <select id="selected_cube" name="selected_cube" onchange="s_file.submit()">
<option value="select">Select Cube</option>

<c:forEach var="i" begin="0" end="<%=c1.getCubes().size()-1%>" step="1">
 <option value='<c:out value="${i}"/>'><c:out value="${ c1.getCubes().get(i).getName()}"/></option>
 </c:forEach>
</select>

<c:if test='<%=cubeValue != "select" %>'>

<c:out value="hi there"/>
</c:if>
</c:if>

</c:if>

</form>

</div>

</body>

Open in new window



Please help
thanks
0
Comment
Question by:Pradip Shenolkar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 40594770
<c:if test='<%=cubeValue != "select" %>'>
That won't work.  What is cubeValue? If it is a scoped variable, then you could try
<c:if test='${cubeValue != "select"}'>

Open in new window

<c:if test='${request.getParameter("selected_file") ne "Select"}' >
That won't work. You could try using.
<c:if test='${param.selected_file  ne "Select"}' >

Open in new window

0
 

Author Comment

by:Pradip Shenolkar
ID: 40595081
I tried your solution but its not working

Here is full code which makes it clear what cubeValue is.(see in scriptlet)

<%
   String fileValue = request.getParameter("selected_file");
   if(fileValue == null)fileValue = "Select File";
   
   String cubeValue = request.getParameter("selected_cube");
   if(cubeValue == null)cubeValue = "Select Cube";

%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script type="text/javascript">

function retainValue(){
     var element1 = document.forms[0].selected_file;
     for(x = 0; x < element1.length; x++){
             if(element1.options[x].value == "<%=fileValue%>"){
                   element1.options[x].selected = true;
                   break;
             } 
     }
     
     var element2 = document.forms[0].selected_cube;
     for(x = 0; x < element2.length; x++){
             if(element2.options[x].value == "<%=cubeValue%>"){
                   element2.options[x].selected = true;
                   break;
             } 
     }
}
</script>
</head>
<body onload="retainValue();">
<%
String folder=application.getRealPath("/WEB-INF/schemas");
File file=new File(folder);
String fileNames[]=file.list();

%>


<jsp:useBean id="c1" class="mypkg.Cubes"></jsp:useBean>

<div id="file">
<form name="s_file" method="post" action="">
<select id="selected_file" name="selected_file" onchange="s_file.submit();">
<option value="Select">Select schema</option>
<c-rt:forEach var="file" items="<%=fileNames%>">
 <option value="<c:out value="${file}"/>"><c:out value="${file}"/></option>
</c-rt:forEach>
</select>

<c:if test='${pageContext.request.method=="POST"}'>
<c:if test='${request.getParameter("selected_file") ne "Select"}' >

<c:set value="<%=fileValue %>" target="${c1}" property="schemaFile"/>
<c:out value="${c1.Connect()}"></c:out>


 <select id="selected_cube" name="selected_cube" onchange="s_file.submit()">
<option value="select">Select Cube</option>

<c:forEach var="i" begin="0" end="<%=c1.getCubes().size()-1%>" step="1">
 <option value='<c:out value="${i}"/>'><c:out value="${ c1.getCubes().get(i).getName()}"/></option>
 </c:forEach>
</select>

<c:if test='${param.selected_cube ne "select" }'>

<c:out value="hi there"/>
</c:if>
</c:if>

</c:if>

</form>

</div>

</body>
</html>

Open in new window

0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 40595154
I stripped out the bean code and put dummy data into your code. I also added page scoped variables that can be  accessed by EL in the JSTL. Does this code do what you want it to do?
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%
   String fileValue = request.getParameter("selected_file");
   if(fileValue == null)fileValue = "Select File";
   pageContext.setAttribute("fileValue", fileValue);
   String cubeValue = request.getParameter("selected_cube");
   if(cubeValue == null)cubeValue = "Select Cube";
   pageContext.setAttribute("cubeValue", cubeValue);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">

function retainValue(){
     var element1 = document.forms[0].selected_file;
     for(x = 0; x < element1.length; x++){
             if(element1.options[x].value == "<%=fileValue%>"){
                   element1.options[x].selected = true;
                   break;
             } 
     }
     
     var element2 = document.forms[0].selected_cube;
     for(x = 0; x < element2.length; x++){
             if(element2.options[x].value == "<%=cubeValue%>"){
                   element2.options[x].selected = true;
                   break;
             } 
     }
}
</script>

</head>
<body onload="retainValue();">
    <div id="file">
    <form name="s_file" method="post" action="">
        <select id="selected_file" name="selected_file" onchange="s_file.submit();">
            <option value="Select File">Select schema</option>
            <c:forEach var="file" items="a,b,c,d">
                <option value="<c:out value="${file}"/>"><c:out value="${file}"/></option>
            </c:forEach>
        </select>
    <c:if test='${fileValue ne "Select File"}' >
        <select id="selected_cube" name="selected_cube" onchange="s_file.submit()">
            <option value="Select Cube">Select Cube</option>
            <c:forEach var="i" begin="0" end="3" step="1">
                <option value='<c:out value="${i}"/>'><c:out value="${i}"/></option>
            </c:forEach>
        </select>
    <c:if test='${cubeValue ne "Select Cube" }'>
        <c:out value="hi there"/>
    </c:if>
    </c:if>
    </form>
    </div>
</body>
</html>

Open in new window

Are you using an old book? I haven't seen <c-rt: used in a very long time.
0
 

Author Closing Comment

by:Pradip Shenolkar
ID: 40595188
It works like charm.

Thank you so much for helping.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question