码迷,mamicode.com
首页 > Web开发 > 详细

JSTL的基本使用

时间:2016-09-04 23:51:57      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

<body>
<%
request.setAttribute("name", "lisi123");
request.setAttribute("ttt", new ArrayList());
request.setAttribute("template", "<h1>lisi123</h1>");
%>
<!--
value:要显示的值
escapeXml:是否转译html标签 true|false
default:默认值 当value的值为null的时候 显示 默认值也可以写在标签体之间 default 和标签体之间的内容不能同时存在


注意 : 最常用的方式是 el表达式


-->
<c:out value="${ttt }" escapeXml="false">123</c:out><br>
${template }

${empty ttt }
<!--
在el表达式中 + 表示加和的意思


-->
${"1"+"2" }
<%-- ${"a"+"b" } --%>
</body>

 

testset.jsp:

<body>

<%!

public static class Users{

private String name;

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}
}


%>
<!--
scope:如果不指定 则默认放到 page作用域中 给定的作用域没有scope结尾

value:存放的值

var:存放的变量

target:要更改的那一个对象

property:要更改的那一个对象中的那一个属性

常用方式
value+var+scope
value+target+property
-->

<%
Users u = new Users();
u.setName("zhangsan");
request.setAttribute("user", u);
%>
<crazy:set scope="request" value="testset123" var="testset"></crazy:set>
<crazy:set scope="page" value="testset123456" var="testset"></crazy:set>
${requestScope.testset }
<hr>
${user.name }

<crazy:set property="name" value="lisi" target="${user }"></crazy:set>
${user.name }

<hr>

<!--
如果 不指定作用域 会将所有作用域中对应名称的值 一除掉

-->
<crazy:remove var="testset" scope="page"/>
${testset }
</body>

 

testif.jsp:

<body>
<c:set value="11" var="num" scope="request"></c:set>
<c:if test="${param.num>10 }" var="flag" scope="page">
<h1 style="color:red">num大于10</h1>
</c:if>
<c:if test="${!flag}">
<h1 style="color:green">num不大于10</h1>
</c:if>


<hr>

<!--
if(){

}else if(){

}else{

}


else{

}if(){

}else{

}else[

}

-->
<c:choose>
<c:when test="${param.num>10 && param.num<20 }"><h1 style="color:red">10 &lt; num &lt; 20</h1></c:when>
<c:when test="${param.num>20 && param.num<50 }"><h1 style="color:red">20 &lt; num &lt; 50</h1></c:when>
<c:when test="${param.num<10 }"><h1 style="color:red">num &lt; 10</h1></c:when>
<c:otherwise><h1 style="color:red">num &gt; 50</h1></c:otherwise>
</c:choose>
</body>

testforeach.jsp:

<body>
<%
List<String> list = new ArrayList<String>();
for(int i=0;i<20;i++){
list.add("list"+i);
}
request.setAttribute("list", list);
%>
<!--
forEach


items:待循环的 集合
var:循环的时候 每次的变量
step:步进或是 间隔
begin:从哪一个下标元素开始
end:在哪一个下标元素结束

-->

<c:forEach items="${list }" var="l" step="2" begin="0" end="10" varStatus="s">
<span>${l }</span>|:|<span>${s.current }|${s.index }|${s.count }|${s.first }|${s.last}</span><br>
</c:forEach>
</body>

JSTL的基本使用

标签:

原文地址:http://www.cnblogs.com/hwgok/p/5840643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!