您的位置:首页 > 编程语言 > Java开发

java web中jsp常用标签

2016-10-16 22:10 585 查看
在jsp页面开发过程中,经常需要使用JSTL(Java Server Pages Standard Tag Library)标签开开发页面,是看起来更加的规整舒服。
JSTL主要提供了5大类标签库:

1.       核心标签库: 为日常任务提供通用支持,如显示和设置变量,重复使用一组项目,测试条件以及其他操作(如导入和重定向web页面等).

2.       国际化(I18N)标签库: 支持多种语言的引用程序.

3.       SQL标签库: 对访问和修改数据库提供标准化支持.

4.       XML标签库: 对XML文件处理和操作提供支持,包括XML节点的解析,迭代,基于XML数据的条件评估以及可扩展样式语言转换(Extensible Style Language Transformations , XSLT)的执行.

5.       函数标签库: 通过在EL表达式中调用函数标签库中的函数来实现特定的操作,例如: ${fn:contains(sring,substring)},其功能是判断string字符串中是否包含substring字符串.

JSTL标签库使用方法:

标签库                         URI                             前缀          使用模式

核心标签库              http://java.sun.com/jstl/core          c             <c:tagname…>

国际化标签(I18N)        http://java.sun.com/jstl/fmt           fmt         <fmt:tagname…>

SQL标签库               http://java.sun.com/jstl/sql           sql          <sql:tagname…>

XML标签库               http://java.sun.com/jstl/xml           x            <x:tagname…>

函数标签库              http://java.sun.com/jstl/functions     fn           <fn:tagname…>

常用的是核心标签库
使用语法:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
常用的标签有
<c:set var="name" value="eric" scope="request"></c:set>
<c:out value="${pageScope.name }" default="<p>中国</p>" escapeXml="false"></c:out><hr>

<c:forEach begin="0" end="2" items="${requestScope.contacts }" step="1" var="stu" varStatus="varstu">
序号:${varstu.count }   姓名:${stu.name }    性别:${stu.gender }     年纪:${stu.age }<br>
</c:forEach>

<c:set var="language" value="-php-java-python-js-" scope="session"></c:set>
<c:forTokens items="${sessionScope.language }" delims="-" var="lan">
${lan }<br/>
</c:forTokens>

<c:remove var=""/>

<c:set var="score" value="90" scope="session"></c:set>
<c:choose>
<c:when test="${score>=90 }">
成绩优秀
</c:when>
<c:when test="${score<90 && score>=80 }">
成绩良
</c:when>
<c:when test="${score>=60 && score<80 }">
成绩及格
</c:when>
<c:otherwise>
成绩不及格
</c:otherwise>
</c:choose>

重定向标签

<c:redirect url="www.baidu.com">
  <c:param name="" value=""></c:param>
</c:redirect>

在jsp页面中如果使用实例标签,需要使用的类符合javaBean规范

1)有无参构造函数

2)属性都是私有的

3)全部属性都有getter/setter

jsp页面中常用的3个方法

<jsp:useBean id="stu" class="pkg2.Student"></jsp:useBean>
<jsp:setProperty property="name" name="stu" value="alex"/>
<jsp:getProperty property="name" name="stu"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: