struts教程笔记3

本文深入探讨了JSTL(JSP Standard Tag Library)标签的使用,包括其在简化JSP页面开发、减少Java代码片段及提高开发效率方面的作用。通过实例展示了条件标签、迭代标签等的使用方法,以及如何在不同作用域中设置和获取数据。

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&gt;request&gt;session&gt;application 
  	//	request.setAttribute

(&quot;abc&quot;,&quot;你好1&lt;a 

href='http://www.baidu.com'&gt;sohu&lt;/a&gt;&quot;);  
  	//	session.setAttribute

(&quot;abc&quot;,&quot;你好2&quot;); 
  	//	application.setAttribute

(&quot;abc&quot;,&quot;你好3&quot;); 
  	//	pageContext.setAttribute

(&quot;abc&quot;,&quot;你好4&quot;); 
  	 
  		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” %>

My JSP 'c_redirect.jsp' starting page

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>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值