一、登陆:
1、LoginTag.java:
package sram.jsp;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class LoginTag extends SimpleTagSupport{
private String userName;
private String userPasswd;
public void setUserName(String userName) {
this.userName = userName;
}
public void setUserPasswd(String userPasswd) {
this.userPasswd = userPasswd;
}
@Override
public void doTag() throws JspException, IOException {
//得到response对象
HttpServletResponse response = (HttpServletResponse)((PageContext)this.getJspContext()).getResponse();
//设置输出内容类型和编码
response.setContentType("text/html;charset=utf-8");
String html = "";
html += "<center><h3>用户登陆页面</h3></center>";
html += "<table border='1' align='center' width='400px'>";
html += " <tr>";
html += " <th>用户名:</th>";
html += " <td><input type='text' name='"+userName+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <th>密码:</th>";
html += " <td><input type='password' name='"+userPasswd+"'/></td>";
html += " </tr>";
html += " <tr>";
html += " <td colspan='2' align='center'><input type='submit' value='登陆'/> <input type='reset' value='重置'/></td>";
html += " </tr>";
html += "</table>";
System.out.println(html);
JspWriter out = this.getJspContext().getOut();
out.write(html);
}
}
2、sram.tld:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<!-- 标签库的版本 -->
<tlib-version>1.1</tlib-version>
<!-- 标签库前缀 -->
<short-name>sram</short-name>
<!-- tld文件的唯一标记 -->
<uri>http://Endeavor.sram.com</uri>
<tag>
<name>logintag</name>
<tag-class>sram.jsp.LoginTag</tag-class>
<body-content>scriptless</body-content>
<!-- 属性声明 -->
<attribute>
<!-- 属性名称 -->
<name>userName</name>
<!-- 是否必填 -->
<required>true</required>
<!-- 是否支持EL表达式 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<!-- 属性名称 -->
<name>userPasswd</name>
<!-- 是否必填 -->
<required>true</required>
<!-- 是否支持EL表达式 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
3、login.jsp:
二、高仿核心标签库:
核心标签库: c:if、c:choose+c:when+c:otherwise、c:forEach
1、实现JSTL标签的if功能:
2、实现JSTL标签的choose+when+otherwise功能:
1)ChooseTag.java
package sram.jsp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ChooseTag extends SimpleTagSupport{
private boolean flag;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void doTag() throws JspException, IOException {
//输出标签体内容,对于choose标签来说,它的子标签都是它的标签体内容
this.getJspBody().invoke(null);
}
}
2)WhenTag.java
package sram.jsp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class WhenTag extends SimpleTagSupport{
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
public void doTag() throws JspException, IOException {
//根据test的返回值决定是否输出标签体内容
if(test){
this.getJspBody().invoke(null);
}
//获取父标签
ChooseTag parent = (ChooseTag)this.getParent();
parent.setFlag(!test);
}
}
3)OtherwiseTag.java
package sram.jsp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class OtherwiseTag extends SimpleTagSupport{
public void doTag() throws JspException, IOException {
//通过父标签传递,when标签中test的值
//获取父标签
ChooseTag parent = (ChooseTag)this.getParent();
boolean test = parent.isFlag();
if(test){
this.getJspBody().invoke(null);
}
}
}
4)sram.tld部分代码:
<tag>
<name>choose</name>
<tag-class>sram.jsp.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>sram.jsp.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>sram.jsp.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
5)choose.jsp及运行结果:
3、实现JSTL标签的foreach功能:
1)ForeachTag.java
package sram.jsp;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForeachTag extends SimpleTagSupport{
private Object items;//需要遍历的数据.List和map
private String var;//每个元素的名称
public void setItems(Object items) {
this.items = items;
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
/*
//遍历items数据
//List
PageContext pageContext = (PageContext)this.getJspContext();
if(items instanceof List){
List list = (List)items;
for (Object object : list) {
//把每个对象放入域对象中(pageContext)
pageContext.setAttribute(var, object);
//显示标签体内容
this.getJspBody().invoke(null);
}
}
//Map
if(items instanceof Map){
Map map = (Map)items;
Set<Entry> entrySet = map.entrySet();
for(Entry entry :entrySet){
//把每个对象放入域对象中(pageContext)
pageContext.setAttribute(var, entry);
//显示标签体内容
this.getJspBody().invoke(null);
}
}
*/
PageContext context = (PageContext)this.getJspContext();
Collection coll = null;
if(items instanceof List){
coll = (Collection)items;
}
if(items instanceof Map){
Map map = (Map)items;
coll = map.entrySet();
}
for(Object object:coll){
//把每个对象放入域对象中(pageContext)
context.setAttribute(var, object);
//显示标签体内容
this.getJspBody().invoke(null);
}
}
}
2)sram.tld
<tag>
<name>foreach</name>
<tag-class>sram.jsp.ForeachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
3)foreach.jsp
<%@ page language="java" import="java.util.*,sram.entity.Person" pageEncoding="utf-8"%>
<%@taglib uri="http://Endeavor.sram.com" prefix="sram" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forEach标签</title>
</head>
<body>
<%
//保存数据
//List
List<Person> list = new ArrayList<Person>();
list.add(new Person("rose",18));
list.add(new Person("jack",28));
list.add(new Person("lucy",38));
//放入域中
pageContext.setAttribute("list",list);
//Map
Map<String,Person> map = new HashMap<String,Person>();
map.put("100",new Person("mark",20));
map.put("101",new Person("maxwell",30));
map.put("102",new Person("narci",40));
//放入域中
pageContext.setAttribute("map",map);
%>
<sram:foreach items="${list}" var="person">
姓名:${person.name } - 年龄:${person.age }<br/>
</sram:foreach>
<hr/>
<sram:foreach items="${map}" var="entry">
编号:${entry.key} - 姓名:${entry.value.name} - 年龄:${entry.value.age }<br/>
</sram:foreach>
</body>
</html>