如果条件匹配,如何检测URL和输出值的一部分?

时间:2022-10-17 15:51:17

I need to create a special condition if the page URL in the browser address bar ends in "search". In my .jpsf file, I have this code so far:

如果浏览器地址栏中的页面URL以“搜索”结尾,我需要创建一个特殊条件。在我的.jpsf文件中,到目前为止我有这个代码:

<c:choose>
    <c:when test="${SOMETHINGHERE eq 'Search'}">
        <c:out value="Search" />
    </c:when>
    <c:otherwise>
        <c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" />
    </c:otherwise>
</c:choose>

What can I put in the SOMETHINGHERE section to detect if the URL contains the word search? Browsing the posts here and searching on Google has not led me to any answers that work so far.

我可以在SOMETHINGHERE部分中检测URL是否包含单词search?在这里浏览帖子并在Google上搜索并没有让我得到任何有效的答案。

I also tried adding <% String currentPDP = request.getParameter("search")%> but it returned a 500 error, probably because _Search is not set up properly as a parameter (it's hard coded at the end of a URL) and the code is not properly formatted.

我还尝试添加<%String currentPDP = request.getParameter(“search”)%>但它返回了500错误,可能是因为_Search没有正确设置为参数(它在URL末尾硬编码)和代码格式不正确。

I am new to JSP and have to learn on the job, so I appreciate your patience and any help you can offer. Thank you kindly.

我是JSP新手,必须在工作中学习,所以我感谢您的耐心和任何帮助。非常感谢你。

1 个解决方案

#1


0  

First you'd want to import the Functions tag with:

首先,您要导入Functions标记:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Then, you can use:

然后,您可以使用:

${fn:contains(pageContext.request.requestURI, 'Search')}

or, if you don't care about the case

或者,如果你不关心这个案子

${fn:containsIgnoreCase(pageContext.request.requestURI, 'Search')}

That will allow you to search through the URI for the term Search.

这将允许您搜索术语搜索的URI。

If the full url to the running script was http://www.example.com/site/search.jsp, then responseURL should return /site/search.jsp. The above examples would return true in the second case because 'Search' is in the URI string (the first would fail due to capitalization).

如果正在运行的脚本的完整网址是http://www.example.com/site/search.jsp,则responseURL应返回/site/search.jsp。上面的例子在第二种情况下会返回true,因为'Search'在URI字符串中(第一种因大小写而失败)。

It does not include query parameters, which would involve a different check.

它不包括查询参数,这将涉及不同的检查。

#1


0  

First you'd want to import the Functions tag with:

首先,您要导入Functions标记:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Then, you can use:

然后,您可以使用:

${fn:contains(pageContext.request.requestURI, 'Search')}

or, if you don't care about the case

或者,如果你不关心这个案子

${fn:containsIgnoreCase(pageContext.request.requestURI, 'Search')}

That will allow you to search through the URI for the term Search.

这将允许您搜索术语搜索的URI。

If the full url to the running script was http://www.example.com/site/search.jsp, then responseURL should return /site/search.jsp. The above examples would return true in the second case because 'Search' is in the URI string (the first would fail due to capitalization).

如果正在运行的脚本的完整网址是http://www.example.com/site/search.jsp,则responseURL应返回/site/search.jsp。上面的例子在第二种情况下会返回true,因为'Search'在URI字符串中(第一种因大小写而失败)。

It does not include query parameters, which would involve a different check.

它不包括查询参数,这将涉及不同的检查。