简体   繁体   English

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

[英]How can I detect portion of URL and output value if condition matches?

I need to create a special condition if the page URL in the browser address bar ends in "search". 如果浏览器地址栏中的页面URL以“搜索”结尾,则需要创建特殊条件。 In my .jpsf file, I have this code so far: 到目前为止,在我的.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? 我可以在SOMETHINGHERE部分中放置什么来检测URL是否包含单词search? Browsing the posts here and searching on Google has not led me to any answers that work so far. 浏览此处的帖子并在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. 我是JSP的新手,必须在工作中学习,因此,感谢您的耐心和能提供的任何帮助。 Thank you kindly. 非常感谢你。

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中Search术语Search

If the full url to the running script was http://www.example.com/site/search.jsp , then responseURL should return /site/search.jsp . 如果正在运行的脚本的完整URL为http://www.example.com/site/search.jsp ,则responseURL应该返回/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). 上面的示例在第二种情况下将返回true,因为URI字符串中包含“ Search”(第一个由于大写而失败)。

It does not include query parameters, which would involve a different check. 它不包括查询参数,查询参数将涉及不同的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何专注于 GoogleMap 的某个部分 - How can I focus on a certain portion of GoogleMap 如何删除链接列表的一部分? - how can I delete a portion of a linked list? 如何验证用户输入是否与 Java 中的数组中的值匹配? - How can I verify that a user input matches a value in an array in Java? 有没有一种方法可以通过一个“匹配”功能检查多个条件? - Is there a way I can check multiple condition with one 'matches' function? 如何在Itext5中填充页面的其余空白部分? - How can I fill the remaining blank portion of the page in Itext5? 我如何打印二维数组的一部分 - How can I print a portion of a 2d array 如何在EditText的值中强制采用数字条件? - How can I enforce a numeric condition in the value of an EditText? 如何获取XML属性并在给定目录java中找到与属性值匹配的文件 - How can I take XML attribute and find a file that matches the attribute value in a given directory java 提示一个字符串值,并检查它是否与特定模式匹配。 如何使用for循环和子字符串方法解决该问题? - Prompting a String value and check if it matches with a specific pattern. How can I solve it by using for loop and substring method? 程序将 output “Invalid Amount, input value is less than 100” 我不知道这个条件怎么写 - Program will output “Invalid Amount, inputted value is less than 100” i don't know how to put that condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM