gpt4 book ai didi

mysql - foreach内部sql :query jSTL

转载 作者:行者123 更新时间:2023-11-29 12:32:44 31 4
gpt4 key购买 nike

我有 2 个 jsp 文件,第一个文件中有一个表单:

        <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>

在第二个jsp中,我从上面的表单中分割了文本

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

然后我想检查数据库中的任何记录是否包含“分割”数组中的任何单词

        <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>

我试图在循环中执行此操作,但这当然不起作用。正确的做法是什么?

最佳答案

感谢您的回答,Sas,我想它会起作用,无论如何我自己解决了它,这是代码:

        <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>

关于mysql - foreach内部sql :query jSTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27233482/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com