我有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, ',')}" />
然后我想检查数据库中的任何记录是否包含“splitted”数组
中的任何单词 <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>
我正在尝试在循环中执行此操作,当然这不起作用。什么是正确的方法?
答案 0 :(得分:1)
Thx for your answer 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>
答案 1 :(得分:0)
试试这样:
<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>
没有测试代码。应该工作..:P