struts标签技术
jstl(jsp standard tag library)
why jstl?
前者jsp文件有大量的<% %> java片段,html+java很乱
1.在应用程序服务器之间提供统一的接口,从而提供了web应用在
不同服务器的移植。
2.简化jsp与web应用程序开发
3.减少jsp中java片段代码,简洁化程序
4.提高jsp开发速度。
目前软件公司是否使用jstl标签?
要求严格与不严格。
一般用途的标签
条件标签
迭代标签
url相关的标签(xml,sql)
jstl实际运用/用户管理系统,信息供求管理系统
一般用途的标签
<%@ page language="java"
import="java.util.*,com.xing.domain.*"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd">
<html>
<head>
<title>My JSP 'c_out.jsp' starting page</title>
</head>
<body>
<%
//如果域对象中有相同的属性名,cout的优先
级是pageContext>request>session>application
// request.setAttribute
("abc","你好1<a
href='http://www.baidu.com'>sohu</a>");
// session.setAttribute
("abc","你好2");
// application.setAttribute
("abc","你好3");
// pageContext.setAttribute
("abc","你好4");
User u=new User();
u.setAge(22);
u.setName("小明");
request.setAttribute("user1",u);
%>
<p>演示c:out标签的使用</p>
<c:out value="hello,world!"></c:out>
<p>如何输出request/sessioin/application/pageContext域对
象的数据</p>
<c:out value="${abc}" default="没有值"
escapeXml="false"></c:out>
<p>演示输出对象值</p>
<c:out value="${user1.name}"></c:out>||<c:out
value="${user1.Age}"></c:out><br/>
${user1.name } @@ ${user1.Age}
<p>演示c:set标签,用于在某个范围
request/sessioin/application/pageContext域设置某个值</p>
<c:set var="abc" value="中国玉林"
scope="request"></c:set>
<c:out value="${abc}"></c:out>
<c:catch var="myexception">
<%int i=8/0; %>
</c:catch>
<c:out value="${myexception}"></c:out>
</body>
</html>
c:if标签的使用
<%@ page language="java"
import="java.util.*,com.xing.domain.*"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'c_if.jsp' starting page</title>
</head>
<body>
<h1>c:if的使用</h1>
<%request.setAttribute("a","helloy");
request.setAttribute("pageNow","56");
User u=new User();
u.setAge(5);
u.setName("老鼠");
request.setAttribute("mouse",u);
%>
<h2>字符串判断</h2>
<c:if test="${a=='hello'}">
ok
</c:if>
<c:if test="${a!='hello'}">
no ok
</c:if>
<h2>数值判断</h2>
<c:if test="${pageNow==56}">
ok
</c:if>
<c:if test="${pageNow!=56}">
no ok
</c:if>
<h2>大小判断</h2>
<c:if test="${pageNow>33}">
pageNow>56
</c:if>
<c:if test="${pageNow>33 and pageNow<59}">
pageNow>33 and pageNow<59
</c:if>
<h2>对象属性判断</h2>
<c:if test="${mouse.age>3}">
老鼠年龄大于三岁
</c:if>
</body>
</html>
迭代标签的使用
<%@ page language="java"
import="java.util.*,com.xing.domain.*"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'c_fortokes.jsp' starting page</title>
</head>
<body>
<h1>c:fortokes</h1>
<c:forTokens items="rencaiguaijiwai;what;你好;清
华小学" delims=";" var="tmp">
${tmp }
</c:forTokens>
</body>
</html>
<%@ page language="java"
import="java.util.*,com.xing.domain.*"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'c_choose.jsp' starting page</title>
</head>
<body>
<h1>c:choose的使用</h1>
<%;
User u=new User();
u.setAge(1);
u.setName("老鼠");
request.setAttribute("duck",u);
%>
<c:choose>
<c:when test="${duck.age<2}">
<font color="red">warning! the duck is to small to
eat.</font>
</c:when>
<c:when test="${duck.age>2}">
<font color="blue">the duck is available,please feel
free to eat</font>
</c:when>
<c:otherwise>
<font color="green">the duck is green so we can't eat
it</font>
</c:otherwise>
</c:choose>
</body>
</html>
jstl细节问题
ArrayList循环迭代,HashMap迭代?
<%@ page language="java"
import="java.util.*,com.xing.domain.*"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme
()+"://"+request.getServerName
()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'c_foreach2.jsp' starting page</title>
</head>
<body>
<h1>对map和set的迭代</h1>
<%
Map map=new HashMap();
// map.put("sunquan","孙权");
// map.put("zhangliao","张辽");
User u1=new User();
u1.setAge(1);
u1.setName("老鼠");
User u2=new User();
u2.setAge(2);
u2.setName("乌鸦");
User u3=new User();
u3.setAge(6);
u3.setName("乌龟");
map.put("mouse",u1);
map.put("wuya",u2);
map.put("turtle",u3);
//map放入某个域对象 session request
pageContext application
//request.setAttribute("person",map);
request.setAttribute("animals",map);
Set set=new HashSet();
set.add(u1);set.add(u2);set.add(u3);
request.setAttribute("myanimals",set);
%>
<c:forEach items="${animals}" var="animal">
key=${animal.key} value=${animal.value.name}
age=${animal.value.age}<br/>
</c:forEach>
<h1>Set集合jstl演示</h1>
<c:forEach items="${myanimals}" var="animal">
value=${animal.name } || age=${animal.age }
<br/>
</c:forEach>
<h1>如何使用jstl if判断集合是否为空</h1>
<c:if test="${!empty myanimals }">有动物</c:if>
<c:if test="${empty myanimals }">没有动物</c:if>
</body>
</html>
jstl-url相关标签
利用param传参数
点击
id=${param.id}
<%@ page language=“java” import=“java.util.*”
pageEncoding=“ISO-8859-1”%>
<%@ taglib prefix=“c”
uri=“http://java.sun.com/jsp/jstl/core” %>
redirect
<%@ page language="java" import="java.util.*"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'c_redirect.jsp' starting page</title>
</head>
<body>
<c:import url="content.jsp">
<c:param name="name" value="xingwen"/>
</c:import>
</body>
</html>
<%@ page language="java" import="java.util.*"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<title>My JSP 'content.jsp' starting page</title>
</head>
<body>
content page ${param.name}<br/>
</body>
</html>