JSTL中的set,out,remove,if,choose,forEach标签的使用

在用于JSTL,需要下载架包     jstl.jar,standard.jar放到项目的WebContent->WEB-INF->lib目录下

在jsp的顶端加上<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

prefix="c"中的c就是标签开头的字母,例<c:set></set>

一通用标签库

    <c:set></set>赋值标签,

     在某个作用域中给变量赋值

    <c:set     var=""      value=""    scope=""></c:set>     三个属性var名字,value值,scope作用域

    可以给存再的变量赋值,也可以给不存在的变量赋值

    <c:set var="name" value="sunday"  scope="request"></c:set>==request.setAttribute("name","sunday");  

<c:set var="name" value="sunday"  scope="request"></c:set>
${requestScope.name}

      在某个作用域之中,给某个对象的属性赋值(此种写法,不能指定scope属性)

       在servlet中

Student student = new Student();
student.setAge(1);
student.setName("a");
request.setAttribute("student", student);
request.getRequestDispatcher("JSTL.jsp").forward(request, response);

       在jsp中

${requestScope.student.name}
<c:set target="${requestScope.student}" property="name" value="b"></c:set>
${requestScope.student.name}

       还可以给map中的对象赋值

       在servlet中

Map<String, String> map =new  HashMap<String, String>();
map.put("a", "b");		
request.setAttribute("map", map);

        在jsp中


${requestScope.map.a} 
<c:set target="${requestScope.map}" property="a" value="c"></c:set>
${requestScope.map.a}

    <c:out>用于显示操作,一共有三个属性value,default ,escapeXml      

       但是要比传统的EL强大
       如果显示的值不存在,EL输出空白
       default,如果显示的值为空,就会自动显示default中的值
       escapeXml当为true的时候,输出标签的去拿不内容,为false的时候,不会把标签输出来

Map<String, String> map =new  HashMap<String, String>();
map.put("a", "b");
request.setAttribute("map", map);
request.getRequestDispatcher("JSTL.jsp").forward(request, response);
<c:out value=" ${requestScope.map.a}" default="null"></c:out>


       <c:out value='<a href="https://baidu.com">百度</a>' escapeXml="true"></c:out>     
       <c:out value='<a href="https://baidu.com">百度</a>' escapeXml="false"></c:out>   

二选择标签

       if 单重选择

       <c:if test="判断条件" scope="request" var="返回值true/false">真 ${requestScope.result}</c:if>

<c:if test="${10>1}" scope="request" var="result">真 ${requestScope.result}</c:if>

       多重选择

       <c:choose> 
                <c:when test=" "></c:when>  test中为判断的条件
                <c:when test=" "></c:when>
                <c:when test=" "></c:when>
                <c:otherwise></otherwise>最终的情况
       </c:choose>

<c:set var="a" value="b" scope="request"></c:set>
<c:choose>
          <c:when test="${requestScope.a=='b' }">
                        值为b
          </c:when>
           <c:when test="${requestScope.a=='c' }">
                        值为c
          </c:when>
          <c:otherwise>其他</c:otherwise>
</c:choose>

      注:test=""后面一定不能有空格,test="${requestScope.a=='b' }" true,test="${requestScope.a=='b' }  "   非true

 

三循环,迭代

    一般用于数组,list,map的输出

     也可以用于最为普通的循环打印

       <c:forEach begin="0" end="5" step="1" varStatus="statue" >
               ${statue.index} test<br>
       </c:forEach>

        begin为开始值,end为结束值,step="1"每次加一,varStatus="statue" ${statue.index}可以输出当前执行到哪一步

     在java中添加一个list转发到,jsp中输出

student stu1 = new Student();
stu1.setName("a");
stu1.setAge(1);
Student stu2 = new Student();
stu2.setName("b");
stu2.setAge(2);
List<Student> list = new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
request.setAttribute("list",list);
request.getRequestDispatcher("JSTL.jsp").forward(request,response);

    jsp

<c:forEach var="name" items="${requestScope.list}">
          name ${name.name} age ${name.age}<br>    
</c:forEach>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值