How to correctly split strings in JSTL?

后端 未结 3 816
萌比男神i
萌比男神i 2020-12-09 17:14

How can I split strings separated by \"/\" inside a jsp page using JSTL?

I have a string in this format: **

\"23/11/2010\"

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 17:44

    You can use the fn:split() function for this.

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

    Be sure that the date format is validated beforehand :) It would be easier if it was a java.util.Date, not a java.lang.String. You could then use to format it to a reliable and fixed string format first. Otherwise you'd need to add checks on array length by fn:length() and to prevent potential XSS attack holes by fn:escapeXml().

    Also important to note is that the function takes a regular expression as argument and not just a plain character sequence. So in case you'd like to split on characters which represent special characters in regex, then you'd need to escape them with backslashes. See also How to split a string in Java for general guidelines which also apply to fn:split().

提交回复
热议问题