Get JSTL tags in ten minutes

I. Introduction

Before writing code, you need to import the dependent jar package

<!-- JSTL表达式依赖 -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- standard标签库 -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

EL expression: ${}

  • retrieve data
  • Perform calculations
  • Get common objects for web development

JSP tags :

<%--
http://localhost:8080/jsptag.jsp?name=xu&age=12
--%>

<jsp:forward page="/jsptag2.jsp">
    <jsp:param name="name" value="xu"/>
    <jsp:param name="age" value="12"/>
</jsp:forward>

2. JSTL tag library:

The use of JSTL tag library is to make up for the deficiencies of HTML tags. It has customized many tags for us to use. The functions of the tags are the same as the java code.

  1. Formatting tags
  2. SQL tags
  3. XML tags
  4. Core tags (master part)

Steps to use JSTL tag library:

  • Introduce the corresponding taglib
  • Use the method
  • Tomcat also needs to introduce the jstl package, otherwise an error will be reported: JSTL parsing error

JSTL tag code display:

<h4>if测试</h4>
<hr>

<form action="coreif.jsp" method="get">
    <%--
    EL表达式获取表单中的数据
    ${
    
    param.参数名}
    --%>
    <input type="text" name="username" value="${param.username}">
    <input type="submit" value="登录">
</form>

<%--判断提交的用户名是管理员就登陆成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
    <c:out value="管理员欢迎你"/>
</c:if>

<c:out value="${isAdmin}"/>
<%--定义一个变量为score,值为85--%>
<c:set var="score" value="55"/>

<c:choose>
    <c:when test="${score >= 90}">
        你的成绩为优秀
    </c:when>

    <c:when test="${score >= 80}">
        你的成绩为一般
    </c:when>


    <c:when test="${score >= 70}">
        你的成绩为良好
    </c:when>

    <c:when test="${score <= 60}">
        你的成绩为不及格
    </c:when>
</c:choose>

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46594796/article/details/109494683