IDEA 中导入 JSTL 库标签

1、在WEB-INF中新建一个lib目录,导入 jstl 标签库的 jar 包

下载链接:  JSTL压缩包.

选择一个最新的版本下载,然后解压到本地。
IDEA 中导入 JSTL 库标签_jsp

在解压后的lib目录中找到:

jstl.jar

standard.jar

然后将这两个文件复制到 WEB-INF 下面的 lib 目录下。

然后右击选择如图(两个都要这样做,目的是添加到工程目录):

IDEA 中导入 JSTL 库标签_java_02

然后选择 OK 就可以了。

IDEA 中导入 JSTL 库标签_JSTL_03

2、在WEB-INF中新建一个 tld 目录,导入需要的 tld 文件

在解压的文件夹下面找到 tld 文件夹,将里面的内容复制到,WEB-INF中的 tld 目录下面

完成之后如下图所示:

IDEA 中导入 JSTL 库标签_java_04

3、在 web.xml 中配置文件信息

配置代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <jsp-config>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt-1_0.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
            <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/c-1_0.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/sql-1_0.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri>
            <taglib-location>/WEB-INF/tld/x.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/x-1_0.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

4、在需要使用 JSTL 标签库的 jsp 页面,使用 taglib 指令引入标签库。

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

全部完成之后我们就可以在 IDEA 中使用 JTSL 标签了。

输出九九乘法表

IDEA 中导入 JSTL 库标签_java_05

实现代码如下:创建一个 nine.jsp 文件,内容如下

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>九九乘法表</title>
</head>
<body>
<table border="1">
    <c:forEach var="i" begin="1" end="9">
        <tr>
            <c:forEach var="j" begin="1" end="${i}">
            <td>${j} * ${i} = ${i * j}</td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>
</body>
</html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

实现简单的输入输出

IDEA 中导入 JSTL 库标签_java_06

实现代码如下:

  1. form.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
    <head>
        <title>选择课程</title>
    </head>
    <body>
    <form action="forEach" method="post">
        请选择你喜欢的课程:<br><br>
        <input type="checkbox" name="course" value="java">java &nbsp;&nbsp;
        <input type="checkbox" name="course" value="jsp">jsp &nbsp;&nbsp;
        <input type="checkbox" name="course" value="xml">xml &nbsp;&nbsp;
        <input type="checkbox" name="course" value="spring">spring<br><br>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
  2. jsp2.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html>
    <head>
        <title>显示选中的课程</title>
    </head>
    <body>
    <h3 id="h0">你选择的课程有:</h3>
    <c:forEach items="${selectedCourses}" var="course">
        <p>${course}</p >
    </c:forEach>
    </body>
    </html>
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
  3. forEachServlet.java

    这个文件放在名为 servlet 的包下面。

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/forEach")
public class forEachServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取选中的课程
        String[] courses = request.getParameterValues("course");

        // 将选中的课程存储在ArrayList中
        ArrayList<String> selectedCourses = new ArrayList<>();
        if (courses != null) {
            for (String course : courses) {
                selectedCourses.add(course);
            }
        }

        // 将ArrayList存储在request属性中
        request.setAttribute("selectedCourses", selectedCourses);

        // 转发到JSP2页面
        request.getRequestDispatcher("jsp2.jsp").forward(request, response);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.