简体   繁体   English

如何使用 JSTL 对数组进行排序?

[英]How can I sort an array with JSTL?

For example if a have this:例如,如果一个有这个:

<form method="post">
    <c:forEach var = "i" begin = "0" end = "5">
        <input type="text" name="textbox">
    </c:forEach>
    <button type="submit" name="buttonSave">Save</button>
</form>
<c:set var="data" value="${paramValues.textbox}"/>
<c:forEach var = "item"  items="${data}">
    ${item}
</c:forEach>

How can I sort my array?如何对数组进行排序? , using JSTL if it's possible. ,如果可能的话,使用 JSTL。

I don't think there's JSTL support for sorting of array or of collection.我不认为 JSTL 支持数组或集合的排序。 You either create your own custom tag to sort or you can just write Java code to perform the sort before processing it.您可以创建自己的自定义标签进行排序,也可以编写 Java 代码在处理之前执行排序。 See example below:请参见下面的示例:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
  <body>
    <form action="index.jsp" method="post">
      <input type="text" name="items"><br>
      <input type="text" name="items"><br>
      <input type="text" name="items"><br>
      <input type="text" name="items"><br>
      <input type="submit" name="submit" value="submit">
    </form>
    <% String[] input = request.getParameterValues("items");
       if (input != null) {
          java.util.Arrays.sort(input);
          request.setAttribute("items", input);
       }
    %>
    <c:forEach var="item" items="${items}">
      <div>${item}</div>
    </c:forEach>
  </body>

Did you put the taglib on the top of jsp?你把taglib放在jsp上面了吗?

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

Actually it's fine to put one (1)其实放一(1)个就好了

but usually we put the other together但通常我们把另一个放在一起

like this像这样

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

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

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM