首页
IT
登录
6mi
u
盘
搜
搜 索
IT
El标签和JSTL总结
El标签和JSTL总结
xiaoxiao
2021-12-14
18
目 录
1 <c:out> ........................................................................................................1 2 <c:set> ........................................................................................................2 3 <c:remove>..................................................................................................3 4 <c:if> ............................................................................................................4 5 <c:forEach>..................................................................................................5 6 <c:import>.....................................................................................................6 7 <c:url> ...........................................................................................................7
1 > <c:out> 功能: <c:out>主要用来显示数据的内容 属性列表: 实例:使用核心标签库,并输出数据 <c:out value="${param.name}" default="welecome jgl to my website!"/> <c:out value="${param.name}">welecome jgl to my website!</c:out> <%--value 属性是必添的,escapeXml 属性是 true 时,不解析特殊字符,false 时,解析特殊 字符--%> <c:out value="${param.name}"ecapeXml="false"><c:out></c:out> 2 <c:set> 功能: <c:set>主要用来将变量存储至 JSP 范围中或是 JavaBean 的属性或 Map 对象中。 属性列表: 实例 1:将变量设置到 Jsp 范围内,并输出 <%--将变量定义在 Jsp 范围内--%> <%--value 属性的两钟使用方式--%> <c:set var="username" value="jack" scope="session"/> <c:set var="pwd" scope="session">000</c:set> <%--通过 el 表达式语言输出--%> ${sessionScope.username} ${sessionScope.pwd} <%--通过 jstl 中<c:out>标签输出--%> <c:out value="${sessionScope.username}"/> 实例 2:将变量设置到 javaBean 对象内,并输出 <jsp:useBean id="stu" class="net.pcedu.student"/> <%--通过<c:set>标签给 javaBean 对象的 age 属性设值--%> <c:set value="16" target="${stu}" property="age"/> <%--输出 javaBean 对象的属性值--%> 年龄:<c:out value="${stu.age}"/> 3 <c:remove> 功能:主要负责移除变量 属性列表: 如: <c:remove var="username" scope="session"/> ${sessionScope.username}—输不出结果 4 <c:if> 功能:主要用于进行 if 判断,如果为 true,则输出标签体中的内容 实例:利用<c:set>标签设置 javaBean 的属性 age,在 jsp 页面中获取 age,如果 age<18,输出相应信息 <%--通过<c:set>标签给 javaBean 对象的 age 属性设值--%> <c:set value="16" target="${stu}" property="age"/> <%--输出 javaBean 对象的属性值--%> 年龄:<c:out value="${stu.age}"/> <%--当 if 判断为 true 时,输出标签体的内容--%> <c:if test="${stu.age<18}" var="young" scope="session">对不起,未成年, 不能访问这个网站...</c:if> <%--输出 if 语句的判断结果--%> 判断结果:<c:out value="${sessionScope.young}"/> 5 <c:forEach> 功能: <c:forEach>为循环控制,它可以将数组,集合(Collection)中的成员循序浏览一 遍。 属性列表: 实例: <%@ page contentType="text/html;charset=gbk"%> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> <%@ page import="java.util.*,net.pcedu.core.UserInfo" %> <%--将 javabean 对象存放到集合中--%> <% List users=new ArrayList(); for(int i=0;i<5;i++) { UserInfo u=new UserInfo(); u.setName("jack-"+i); u.setPwd("00"+i); users.add(u); session.setAttribute("users",users); } %> <%--注意:EL中只可通过 11 个隐含对象来输出表达式中的内容(因此直接将 List对象添加 到 EL 表达式中不可用)--%> <%--通过<c:forEach>迭代出集合中的信息>--%> <h1>用户信息</h1> <table> <tr> <th>用户名</th> <th>密码</th> <th>当前行的索引</th> <th>已遍历的行数</th> <th>是否第一行</th> <th>是否最后一行</th> </tr> <c:forEach var="user" items="${users}" varStatus="status"><%--加上 begin="1" end="3" step="1"属性,将只显示前三条记录--%> <tr> <td><c:out value="${user.name}"/></td> <td><c:out value="${user.pwd}"/></td> <td><c:out value="${status.index}"/></td><%--输出当前行的索引号--%> <td><c:out value="${status.count}"/></td><%--输出已遍历的行数--%> <td><c:out value="${status.first}"/></td><%--输出当前行是否是第一行 --%> <td><c:out value="${status.last}"/></td><%--输出当前行是否是最后一行 --%> </tr> </c:forEach> </table> <%--通过<c:forEach>输出从 1 到 10 的数据--%> <c:forEach var="num" begin="1" end="10" step="2"> <c:out value="->${num}"></c:out> </c:forEach> <%--通过<c:forEach>遍历数组,枚举,集合等--%> <% int[]intarr=new int[]{10,20,30,40,50}; String[]strarr=new String[]{"I","am","a","handsome","boy"}; Vector v=new Vector(); v.add("This"); v.add("is"); v.add("a"); v.add("Enumeration"); v.add("example"); Enumeration e=v.elements(); HashMap h=new HashMap(); h.put("hello","0"); h.put("hello1","1"); h.put("hello2","2"); h.put("hello3","3"); h.put("hello4","4"); request.setAttribute("intarr",intarr); request.setAttribute("strarr",strarr); request.setAttribute("e",e); request.setAttribute("h",h); %> <br> <h1>--遍历整形数组--</h1> <%--遍历整形数组--%> <c:forEach var="i" items="${intarr}"> <c:out value="${i}"></c:out> </c:forEach> <br> <h1>--遍历字符串数组--</h1> <%--遍历字符串数组--%> <c:forEach var="s" items="${strarr}"> <c:out value="${s}"></c:out> </c:forEach> <br> <h1>--遍历枚举--</h1> <%--遍历枚举--%> <c:forEach var="ee" items="${e}"> <c:out value="${ee}"></c:out> </c:forEach> <br> <h1>--遍历 HashMap--</h1> <%--遍历 HashMap--%> <c:forEach var="hh" items="${h}"> <c:out value="${hh.key}"/>=<c:out value="${hh.value}"/> </c:forEach> 6 <c:import> 功能: <c:import>可以把其他静态或动态文件包含至本身 JSP 网页。 属性列表: 注意:<c:import>与<jsp:include>的区别 <jsp:include>只能包含和自己同一个 Web 应用程序下的文件;而<c:import>除了能 包含和自己同一个 Web 应用程序的文件外,亦可以包含不同 Web 应程序或者是其 它网站的文件。 实例:包含同一个 web 应用程序的文件和不同 webweb 应用程序的文件 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <%@ page contentType="text/html; charset=gb2312" language="java" %> <%--引入绝对路径的文件--%> <%--注意:被引入的文件将被解析为 html 的形式嵌入引用文件--%> <h1>引入绝对路径的文件</h1> <c:import url="http://127.0.0.1:8080/test/c_beimported.jsp" var="file" charEncoding="gbk"/> <blockquote> <pre> <c:out value="${file}"></c:out> </pre> </blockquote> <%--引入相对路径的文件--%> <h1>引入相对路径的文件</h1> <blockquote> <pre> <c:import url="c_beimported.jsp" var="f"/> <c:out value="${f}"></c:out> </pre> </blockquote> <%--传递参数到被引入文件--%> <h1>传递参数到被引入文件</h1> <blockquote> <pre> <c:import url="c_beimported.jsp" var="ff"> <c:param name="name" value="jack"/> </c:import> <c:out value="${ff}"></c:out> </pre> </blockquote> 7 <c:url> 功能: <c:url>主要用来产生一个 URL 放到一个变量中,并输出 url <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <%@ page contentType="text/html; charset=gb2312" language="java" %> <%--将一个 url 存放到一个变量中,并输出 url--%> <c:url var="myurl" value="c_beimported.jsp" scope="session"> <c:param name="name" value="jgl"/> </c:url> <c:out value="${myurl}"/>
转载请注明原文地址: https://ju.6miu.com/read-970486.html
专利
最新回复
(
0
)