JSTLif标签和choose标签的使⽤及介绍案例JSTL
概念:javaServer page Tag library jsp标准标签库
是由Apache组织提供的开源的免费的jsp标签
作⽤:⽤于简化和替换jsp页⾯上的java代码
使⽤步骤:
1.导⼊jstl相关的jar包
2.引⼊标签库:taglib指令 <%@taglib %>
3.使⽤标签
常⽤的JSTL标签
1.if :相当于java代码的if语句
1.属性:
test 必须属性,接受boolean表达式
如果表达式为true,则显⽰if标签体内容,如果为false,则不显⽰标签体内容
⼀般情况下,test属性值会结合el表达式⼀起使⽤
注意:c:if标签没有else情况,想要else情况,则可以再定义⼀个c:if标签
<%
User user =new User();
user.setName("⼩徐");
user.setAge(21);
request.setAttribute("user",user);
%>
<h2>if标签的使⽤</h2>
<c:if test="${not empty user}">
${user.name}<br>
${user.age}<br>
</c:if>
<c:if test="true">
if标签的使⽤
</c:if>
2.choose :相当于java代码的switch语句
1.使⽤choose标签声明相当于switch⽣命
2.使⽤when标签做判断相当于case
3.使⽤otherwise标签做其他情况的声明相当于default
<% request.setAttribute("number",7)%>
<h1>choose标签的使⽤</h1>
<c:choose>
<c:when test="${number == 1}">星期⼀</c:when>
<c:when test="${number == 2}">星期⼆</c:when>
<c:when test="${number == 3}">星期三</c:when>
<c:when test="${number == 4}">星期四</c:when>
<c:when test="${number == 5}">星期五</c:when>
<c:when test="${number == 6}">星期六</c:when>
<c:when test="${number == 7}">星期⽇</c:when>
<c:otherwise>⽇期格式不对</c:otherwise>
</c:choose>
el表达式获取值项⽬准备:
⼀:导⼊相关依赖使⽤的
<!--javaEE-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- mvnrepository/artifact/javax.servlet.jsp.jstl/jstl --> <dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--jstl依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
前端页⾯代码:
<%@ page import="ity.User"%>
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date:2020/3/6
Time:19:53
To change this template use File | Settings | File Templates.
-
-%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%> <%@ taglib prefix="c" uri="java.sun/jsp/jstl/core"%>
<html>
<head>
<title>if标签的使⽤</title>
</head>
<body>
<%
User user =new User();
user.setName("⼩徐");
user.setAge(21);
request.setAttribute("user", user);
request.setAttribute("number",7);
%>
<h2>if标签的使⽤</h2>
<c:if test="${not empty user}">
${user.name}<br>
${user.age}<br>
</c:if>
<c:if test="true">
if标签的使⽤
</c:if>
<h1>choose标签的使⽤</h1>
<c:choose>
<c:when test="${number == 1}">星期⼀</c:when>
<c:when test="${number == 2}">星期⼆</c:when>
<c:when test="${number == 3}">星期三</c:when>
<c:when test="${number == 4}">星期四</c:when>
<c:when test="${number == 5}">星期五</c:when>
<c:when test="${number == 6}">星期六</c:when>
<c:when test="${number == 7}">星期⽇</c:when>
<c:otherwise>⽇期格式不对</c:otherwise>
</c:choose>
</body>
</html>
演⽰效果如图:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论