简体   繁体   中英

foreach inside sql:query jstl

I've got 2 jsp files, in the first one I've got a form:

        <form action="searchresult" method="POST"> Words seperated by commas <br><br><br>
            Enter words you look for: <input type="text" name="searchText" value="" />
            <button type="submit" name="titleSearch" value="costam">Search in title</button>
            <button type="submit" name="contentSearch">Search in content</button>
        </form>  

In the second jsp I split the text from the form above

        <c:set var="searchText" value="${param.searchText}"/>
        <c:set var="splitted" value="${fn:split(searchText, ',')}" />

and then I want to check if any record from database contains any of words from "splitted" array

        <c:if test="${not empty param.titleSearch}">
            <sql:query var="result1" dataSource="jdbc/myProba">
                SELECT title, shortcon FROM bazaprojekt.ad WHERE
                    <c:forEach items="${splitted}" var="word">
                        title LIKE ? <sql:param value='${word}'/> OR
                    </c:forEach>
            </sql:query>
        </c:if>
            <c:forEach var="row" items="${result1.rows}">  
                <div class="adtitle">
                    <a href="singlead?idad=<c:out value="${row.idad}"/>"><c:out value="${row.title}"/></a>                        
                    <p>
                    <c:out value="${row.shortcon}"/>
                    </p>
                </div>
            </c:forEach> 

I'm trying to do this in a loop which of course doesn't work. What is the correct way?

Thx for your answer Sas, i guess it will work, anyway i solved it myself, here is the code:

        <c:if test="${not empty param.titleSearch}">
            <sql:query var="result1" dataSource="jdbc/myProba">
                SELECT idad, title, shortcon FROM bazaprojekt.ad WHERE
                <c:if test="${fn:length(splitted) == 1}">
                    <c:forEach items="${splitted}" var="word">
                        title LIKE '%<c:out value='${word}'/>%'
                    </c:forEach>
                </c:if>
                <c:if test="${fn:length(splitted)  > 1}">
                    <c:forEach items="${splitted}" var="word" begin="0" end="${fn:length(splitted) -2}">
                        title LIKE '%<c:out value='${word}'/>%' OR
                    </c:forEach>
                    <c:forEach items="${splitted}" var="word" begin="${fn:length(splitted) -1}" end="${fn:length(splitted) -1}">
                        title LIKE '%<c:out value='${word}'/>%'
                    </c:forEach>                            
                </c:if>                            
            </sql:query>
        </c:if>

Try like this:

<c:set var="count" value="${fn:length(splitted)}" />

<c:if test="${not empty param.titleSearch}">
  <sql:query var="result1" dataSource="jdbc/myProba">
     SELECT title, shortcon FROM bazaprojekt.ad WHERE
     title LIKE 
   <c:forEach begin="1" end="${count}" varStatus="i">
     ?
   </c:forEach>
   <c:forEach begin="1" end="${count}" varStatus="index">
      <sql:param value='${splitted[index]}'>
   </c:forEach>
  </sql:query>
</c:if>

didn't tested the code. Should work.. :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM