现在的位置: 首页 > 综合 > 正文

JSTL(jsp标准标签库)常用标签用法

2013年08月29日 ⁄ 综合 ⁄ 共 3877字 ⁄ 字号 评论关闭
 
 
在JSP页面中声明JSTL标签:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>
<%@taglib prefix="i18n" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/function"%>

核心标签库

  1、流程控制:if,choose,when,otherwise

<c:if test="(这里放一个EL表达式)">xx</c:if>

 

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="${!empty param.color}">
    
<c:choose>
        
<c:when test="${param.color == 'red'}">
            
<table bgcolor="red"><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table>
        
</c:when>
        
<c:when test="${param.color == 'yellow'}">
            
<table bgcolor="yellow"><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table>
        
</c:when>
        
<c:when test="${param.color == 'blue'}">
            
<table bgcolor="blue"><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table>
        
</c:when>
        
<c:otherwise>
            
<h2>No choice</h2>
        
</c:otherwise>
    
</c:choose>
</c:if>
<form>
<input type="radio" name="color" value="red"><font color="red">red</font></input><br>
<input type="radio" name="color" value="yellow"><font color="yellow">yellow</font></input><br>
<input type="radio" name="color" value="blue"><font color="blue">blue</font></input><br>
    
<input type="submit"/>
</form>

 

  2、迭代:forEach

 遍历集合

<c:forEach var="i" items="${collection}" varStatus="index">

    ${i} ${index}  <!--i为集合中单个元素,index为循环状态 -->

</c:forEach>

遍历Map

<c:forEach var="m" items="${map}">

  key=${m.key},value=${m.value}

</c:forEach>

打印1到10

<c:forEach var="i" begin="1" end="10" step="1">

  ${i}

</c:forEach> 

<%@page import="tarena.jstl.Student,java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
    Collection students 
= new ArrayList();
    students.add(
new Student("001","zhangsan",23));
    students.add(
new Student("002","lisi",22));
    students.add(
new Student("003","wangwu",21));
    
    request.setAttribute(
"stus", students);
%>
<table border="1">
<c:forEach var="stu" items="${stus}">
    
<tr><td>${stu.id}</td>
        
<td>${stu.name}</td>
        
<td>${stu.age}</td>
    
</tr>
</c:forEach>
</table>
<h2>
<%
    Map stus 
= new HashMap();
    stus.put(
"001"new Student("001","zhangsan",23));
    stus.put(
"002"new Student("002","lisi",22));
    stus.put(
"003"new Student("003","wangwu",21));
    
    request.setAttribute(
"stumap", stus);
%>
<c:forEach var="stu" items="${stumap}">
    ${stu.value.id} ${stu.value.name} ${stu.value.age}
<br>
</c:forEach>
<hr>
<c:forEach items="${stus}" varStatus="status">
    ${status.count} ${status.current.name} ${status.current.age}
<br>
</c:forEach>
<hr>
<c:forEach var="i" begin="0" end="2" step="1">
    No.${i} ${stus[i].id} ${stus[i].name} ${stus[i].age}
<br>
</c:forEach>
</h2>

 

C     变量,流控制,url管理       核心
X                  流控制                  xml
fmt 日期消息格式,区域设置 I18n
sql                  SQL                  数据库
fn                  字符串操作         函数

变量支持标签:
 
<c:set var="foo" scope="session" value="ABC"/>
等同于
 
<%session.setAttribute("foo","ABC");%>
 scope 是活动范围 可以是 page request session application

 
<c:remove var="foo" scope="request">
等同于
 
<%request.removeAttribute("foo");%>

流程控制标签:
 
<c:if test="条件判断boolean值">
 ...执行流程...
 
</c:if>
 
<c:choose>
  
<c:when test="条件判断boolean值">...执行流程...</c:when>
  
<c:when test="条件判断boolean值">...执行流程...</c:when>
  
<c:otherwise>...执行流程...</c:otherwise>
 
</c:choose>
 
<c:forEach var="变量名" items="集合">
 
</c:forEach>
输出:
 
<c:out value="输出值"/>
SQL:
 
<sql:setDataSource dataSource="JNDI名" var="source名字"/>
或者
<sql:setDataSource var="source" driver="" url="" user="" password=""/>

 
<sql:query var="返回集合名" dataSource="source名字">
  select 
* from books where id=?
  
<sql:param value=""/>
 
</sql:query>
 
<sql:query var="返回集合名" dataSource="source名字" sql="select * from admin"/>

 
<sql:update var="变量名" dataSource="source名字">
  update books 
set name=

抱歉!评论已关闭.