您的位置:首页 > Web前端 > JavaScript

JSTL使用实例

2015-12-17 17:13 656 查看
<%@page import="java.util.List"%>

<%@page import="java.util.ArrayList"%>

<%@ page language="java" contentType="text/html; charset=utf-8"%>

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

<<jsp:useBean id="person" class="com.ftc.test.Person"></jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>
<h1>this is index.jsp!</h1>
<hr>

<%
String username = "ftc";
request.setAttribute("username", username);
%>

<c:out value="this is our JSTL firstDemo"></c:out>
<hr>

<input type="text" value="username" id="username" name="username" />
<input type="submit" value="submit" id="submit" name="submit" />

<c:out value="${username}">
</c:out>
<br>
<c:out value="${1+2}"></c:out>
<c:out value="${empty username}"></c:out>
<br>

<c:out value="${name }" default="error"></c:out>
<br>

<!-- 存值到scope (存储一个value的常量到var这么变量中,作用于为scope) -->
<c:set value="today" var="day" scope="session"></c:set>
<c:out value="${day }"></c:out>
<br>

<c:set var="age" scope="application">eleven</c:set>
<c:out value="${age}"></c:out>

<c:set target="${person}" property="name" value="zhangsan"></c:set>
<c:out value="${person.name}"></c:out>

<!-- remove 标签的用法 -->
<c:set var="firstname" value="vlay"></c:set>
<c:out value="${firstname }"></c:out>
<c:set var="lastname" value="wang"></c:set>
<c:remove var="lastname" />
<c:out value="${lastname }"></c:out>
<hr>

<!-- catch标签的用法 -->
<c:catch var="error">
<c:set target="aa" property="bb">asas</c:set>
</c:catch>
<c:out value="${error}"></c:out>

<!-- if标签的用法 -->
<form action="index.jsp" method="post">
<input type="text" name="score" value="${param.score}" /> <input
type="submit" />
</form>

<c:if test="${param.score>=90}" var="result" scope="application">
<c:out value="你的成绩是优秀"></c:out>
<br>
</c:if>
<c:out value="${applicationScope.result}"></c:out>
<hr>

<!-- choose when otherwise标签的使用 -->
<c:choose>
<c:when test="${param.score>=90 && param.score<=100}">
<c:out value="优秀"></c:out>
</c:when>
<c:when test="${param.score>=80 && param.score<90}">
<c:out value="良"></c:out>
</c:when>
<c:when test="${param.score>=70 && param.score<80}">
<c:out value="中等"></c:out>
</c:when>
<c:when test="${param.score>=60 && param.score<70}">
<c:out value="及格"></c:out>
</c:when>
<c:when test="${param.score>=0 && param.score<60}">
<c:out value="不及格"></c:out>
</c:when>
<c:otherwise>
<c:out value="你的输入有误!"></c:out>
</c:otherwise>
</c:choose>

<c:choose>
<c:when test="${param.score==100}">
<c:out value="你是满分"></c:out>
</c:when>
</c:choose>
<br>

<%
List<String> fruits = new ArrayList<String>();
fruits.add("苹果");
fruits.add("橘子");
fruits.add("香蕉");
fruits.add("葡萄");
fruits.add("梨子");
request.setAttribute("fruits", fruits);
%>

<!-- foreach标签的使用 -->
<c:forEach var="fruit" items="${fruits}">
<c:out value="${fruit}"></c:out>
<br>
</c:forEach>
<hr>

<c:forEach var="fruit" items="${fruits}" begin="1" end="3">
<c:out value="${fruit}"></c:out>
<br>
</c:forEach>
<hr>

<c:forEach var="fruit" items="${fruits}" step="2">
<c:out value="${fruit}"></c:out>
<br>
</c:forEach>
<hr>

<c:forEach var="fruit" items="${fruits}" varStatus="fru">
<c:out value="${fruit}的四个属性值"></c:out>
<c:out value="index:${fru.index}"></c:out>
<c:out value="count:${fru.count}"></c:out>
<c:out value="first:${fru.first}"></c:out>
<c:out value="last:${fru.last}"></c:out>
<br>
</c:forEach>
<hr>

</body>
</html>

最后输出为:

<

this is index.jsp!

this is our JSTL firstDemo
  ftc 
3 false 
error 
today 
eleven zhangsan vlay
javax.servlet.jsp.JspTagException: Invalid property in <set>: "bb"

 
你的成绩是优秀 
true
优秀 你是满分 
苹果 
橘子 
香蕉 
葡萄 
梨子 
橘子 
香蕉 
葡萄 
苹果 
香蕉 
梨子 
苹果的四个属性值 index:0 count:1 first:true last:false 
橘子的四个属性值 index:1 count:2 first:false last:false 
香蕉的四个属性值 index:2 count:3 first:false last:false 
葡萄的四个属性值 index:3 count:4 first:false last:false 
梨子的四个属性值 index:4 count:5 first:false last:true 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: