• jsp JSTL标签详细示例(c:if与c:forEach)


    JSTL标准标签库(jsp Standarded Tag Library),使用标签来取代jsp页面上的Java代码

    JSTL标签库有很多,我们只学两个,if标签的替代和for循环标签的替代

    JSTL快速入门(使用步骤)

    1.导入坐标


        jstl
        jstl
        1.2


        taglibs
        standard
        1.1.2

    2.在jsp页面页面上引入JSTL标签库

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

    3.使用

    1.c:if标签  相当于Java中的if else标签

    (我们结合el标签来进行判断,从servlet中获取值,在jsp文件中进行判断)

    实体类brand

    1. package com.ithema.pojo;
    2. /*
    3. 在实体类中,基本数据类型,建议使用其对应的包装类
    4. */
    5. public class Brand {
    6. //id主键
    7. private Integer id;
    8. //品牌名称
    9. private String brandName;
    10. //企业名称
    11. private String companyName;
    12. //排序字段
    13. private Integer ordered;
    14. //描述信息
    15. private String description;
    16. //状态 0:禁用 1:启用
    17. private Integer status;
    18. public Brand(Integer id, String brandName, String companyName, Integer ordered, String description,
    19. Integer status) {
    20. super();
    21. this.id = id;
    22. this.brandName = brandName;
    23. this.companyName = companyName;
    24. this.ordered = ordered;
    25. this.description = description;
    26. this.status = status;
    27. }
    28. public Brand() {
    29. }
    30. @Override
    31. public String toString() {
    32. return "Brand [id=" + id + ", brandName=" + brandName + ", companyName=" + companyName + ", ordered=" + ordered
    33. + ", description=" + description + ", status=" + status + "]";
    34. }
    35. public Integer getId() {
    36. return id;
    37. }
    38. public void setId(Integer id) {
    39. this.id = id;
    40. }
    41. public String getBrandName() {
    42. return brandName;
    43. }
    44. public void setBrandName(String brandName) {
    45. this.brandName = brandName;
    46. }
    47. public String getCompanyName() {
    48. return companyName;
    49. }
    50. public void setCompanyName(String companyName) {
    51. this.companyName = companyName;
    52. }
    53. public Integer getOrdered() {
    54. return ordered;
    55. }
    56. public void setOrdered(Integer ordered) {
    57. this.ordered = ordered;
    58. }
    59. public String getDescription() {
    60. return description;
    61. }
    62. public void getDescription(String description) {
    63. this.description = description;
    64. }
    65. public Integer getStatus() {
    66. return status;
    67. }
    68. public void setStatus(Integer status) {
    69. this.status = status;
    70. }
    71. }

    servlet-el  servlet文件

    1. package com.ithema.web;
    2. import com.ithema.pojo.Brand;
    3. import javax.servlet.*;
    4. import javax.servlet.http.*;
    5. import javax.servlet.annotation.*;
    6. import java.io.IOException;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. @WebServlet("/demo1")
    10. public class Servlet_el extends HttpServlet {
    11. @Override
    12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    13. //1.准备数据 现在是list集合,以后是数据库
    14. List brands= new ArrayList();
    15. brands.add(new Brand(1,"小米公司","小米科技有限公司",100,"为发烧而生",1));
    16. //2.储存带request域中进行转发
    17. request.setAttribute("brands",brands);
    18. request.setAttribute("status",1);
    19. //3.进行请求转发
    20. // request.getRequestDispatcher("el-demo.jsp").forward(request,response);
    21. request.getRequestDispatcher("jstl-demo.jsp").forward(request,response);
    22. }
    23. @Override
    24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    25. this.doGet(request, response);
    26. }
    27. }

    jstl-demo.jsp文件

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <html>
    4. <head>
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <%-- c:if:用来完成逻辑判断,替换java if else
    9. 1.一般用法是用el表达式来获取值
    10. 2.再用jstl标签上的c:if来判断值是否合乎规则
    11. --%>
    12. <%--<c:if test="true">
    13. <h1> true h1>
    14. c:if>
    15. <c:if test="false">
    16. <h1> false h1>
    17. c:if>--%>
    18. <c:if test="${status ==1}">
    19. <h2>启用status字符串h2>
    20. c:if>
    21. <c:if test="${status !=1}">
    22. <h2>禁用status字符串h2>
    23. c:if>
    24. body>
    25. html>

    pom文件 

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>jsp-demo01artifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>18maven.compiler.source>
    11. <maven.compiler.target>18maven.compiler.target>
    12. properties>
    13. <packaging>warpackaging>
    14. <build>
    15. <plugins>
    16. <plugin>
    17. <groupId>org.apache.tomcat.mavengroupId>
    18. <artifactId>tomcat7-maven-pluginartifactId>
    19. <version>2.2version>
    20. plugin>
    21. <plugin>
    22. <groupId>org.apache.maven.pluginsgroupId>
    23. <artifactId>maven-compiler-pluginartifactId>
    24. <configuration>
    25. <source>17source>
    26. <target>17target>
    27. configuration>
    28. plugin>
    29. plugins>
    30. build>
    31. <dependencies>
    32. <dependency>
    33. <groupId>javax.servletgroupId>
    34. <artifactId>javax.servlet-apiartifactId>
    35. <version>3.1.0version>
    36. <scope>providedscope>
    37. dependency>
    38. <dependency>
    39. <groupId>commons-iogroupId>
    40. <artifactId>commons-ioartifactId>
    41. <version>2.6version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.mybatisgroupId>
    45. <artifactId>mybatisartifactId>
    46. <version>3.5.5version>
    47. dependency>
    48. <dependency>
    49. <groupId>mysqlgroupId>
    50. <artifactId>mysql-connector-javaartifactId>
    51. <version>8.0.29version>
    52. dependency>
    53. <dependency>
    54. <groupId>junitgroupId>
    55. <artifactId>junitartifactId>
    56. <version>4.13.2version>
    57. <scope>Testscope>
    58. dependency>
    59. <dependency>
    60. <groupId>org.slf4jgroupId>
    61. <artifactId>slf4j-apiartifactId>
    62. <version>1.7.36version>
    63. dependency>
    64. <dependency>
    65. <groupId>ch.qos.logbackgroupId>
    66. <artifactId>logback-classicartifactId>
    67. <version>1.2.3version>
    68. dependency>
    69. <dependency>
    70. <groupId>ch.qos.logbackgroupId>
    71. <artifactId>logback-coreartifactId>
    72. <version>1.2.3version>
    73. dependency>
    74. <dependency>
    75. <groupId>jstlgroupId>
    76. <artifactId>jstlartifactId>
    77. <version>1.2version>
    78. dependency>
    79. <dependency>
    80. <groupId>taglibsgroupId>
    81. <artifactId>standardartifactId>
    82. <version>1.1.2version>
    83. dependency>
    84. dependencies>
    85. project>

    效果

    2.c:forEach标签  相当于Java中的foreach循环

    1.:相当于for循环

    *items:被遍历的容器

    *var:遍历产生的临时变量

    *varStatus:遍历状态对象

            *index属性 从0开始计数

            *count属性 从1开始计数

        (例如,在数据库中的id可能会有断层,而用数据库中的id当作序号的话,会在显示页面造成断层,从而对阅读者产生不好的影响,所以就会使用varStatus这么一个方法)

    示例:


       
            ${brand.id}
            ${brand.brandName}
            ${brand.companyName}
            ${brand.ordered}
            ${brand.description}
            ${brand.status}
       

    2.${brand.id}代表的不是实体类中的属性变量的名称,而是找到getID这个方法,获取值

    实体类brand(和上面的是一样的,所以就不在写了) 

     servlet-el  servlet文件

    1. package com.ithema.web;
    2. import com.ithema.pojo.Brand;
    3. import javax.servlet.*;
    4. import javax.servlet.http.*;
    5. import javax.servlet.annotation.*;
    6. import java.io.IOException;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. @WebServlet("/demo1")
    10. public class Servlet_el extends HttpServlet {
    11. @Override
    12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    13. //1.准备数据 现在是list集合,以后是数据库
    14. List brands= new ArrayList();
    15. brands.add(new Brand(1,"小米公司","小米科技有限公司",100,"为发烧而生",1));
    16. brands.add(new Brand(2,"华为","华为科技有限公司",10,"不为发烧而生",0));
    17. brands.add(new Brand(3,"锤子","锤子科技有限公司",99,"为破产而胜",1));
    18. //2.储存带request域中进行转发
    19. request.setAttribute("brands",brands);
    20. request.setAttribute("status",1);
    21. //3.进行请求转发
    22. // request.getRequestDispatcher("el-demo.jsp").forward(request,response);
    23. // request.getRequestDispatcher("jstl-demo.jsp").forward(request,response);
    24. request.getRequestDispatcher("jstl-for-demo.jsp").forward(request,response);
    25. }
    26. @Override
    27. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    28. this.doGet(request, response);
    29. }
    30. }

     jstl-for-demo.jsp文件

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: HP
    4. Date: 2022/8/20
    5. Time: 16:21
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    10. <html>
    11. <head>
    12. <title>Titletitle>
    13. head>
    14. <body>
    15. <table border="1" cellspacing="0" width="800">
    16. <tr>
    17. <th>序号th>
    18. <th>品牌名称th>
    19. <th>企业名称th>
    20. <th>排序th>
    21. <th>品牌介绍th>
    22. <th>状态th>
    23. <th>操作th>
    24. tr>
    25. <c:forEach items="${brands}" var="brand" varStatus="status">
    26. <tr align="center">
    27. <%-- <td>${brand.id}td>--%>
    28. <td>${status.count}td>
    29. <td>${brand.brandName}td>
    30. <td>${brand.companyName}td>
    31. <td>${brand.ordered}td>
    32. <td>${brand.description}td>
    33. <c:if test="${brand.status == 0 }">
    34. <td>禁用td>
    35. c:if>
    36. <c:if test="${brand.status == 1 }">
    37. <td>启用td>
    38. c:if>
    39. <td><a href="#">修改a>/<a href="#">删除a>td>
    40. tr>
    41. c:forEach>
    42. table>
    43. body>
    44. html>

    效果

    3. c:forEach标签  相当于Java中的for循环(简单for循环)

        *begin:开始数

        *end :结束数

        *step :步长

    示例:


       

    for循环



    相当于

    for(int i = 0,i<=10,i++){

    system.out.println(”for循环“);

    }

    效果 

  • 相关阅读:
    数据结构学习笔记——插入排序
    基于json文件创建后端模拟接口
    论文阅读笔记《Locality Preserving Matching》
    一种简易的Nor flash存储数据机制
    Hbase底层原理简介(一)
    YOLOV1详解——Pytorch版
    Python 有哪些流行的开发框架和库?
    uniapp移动端实现上拉加载(分页),下拉刷新
    基于VGG16的猫狗数据集分类
    配置git并把本地项目连接github
  • 原文地址:https://blog.csdn.net/qq_51272114/article/details/126440607