Implementation method for jsp to judge whether list includes string

  • 2021-10-16 02:24:59
  • OfStack

In jstl, there are methods such as fn: contains (str, str) to judge whether a string contains another string, but there is no method to judge whether list contains string, so I wrote a method myself


<%@ tag import="java.util.List" %>
<%@ tag import="org.apache.commons.lang3.StringUtils" %>
<%@ tag import="java.util.Arrays" %>
<%@tag pageEncoding="UTF-8"%>
<%@ attribute name="srcStr" type="java.lang.String" required="true"%>
<%@ attribute name="separatorChars" type="java.lang.String" required="false"%>
<%@ attribute name="str" type="java.lang.String" required="true"%>
  String sepChars=separatorChars==null?"-":separatorChars;
  String[] splitChars = StringUtils.split(srcStr, sepChars);
  List<String> list = Arrays.asList(splitChars);

Then add a sentence to the page where you need to use this method:

<%@ taglib prefix="gms" tagdir="/WEB-INF/tags" %>

So you can use it directly

<gms:contains srcStr="${srcStr }" str="${str }"> str </gms:contains>

In this way, we can judge.

Related articles: