1、继承助手类
public class ForeachTag extends BodyTagSupport {
}
2、开始编码
private List
1、继承助手类
public class IfTag extends BodyTagSupport{
}
2、开始编码
private boolean test;
//传入一个参数,这是一个判断的结果
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
if(test) {
return EVAL_BODY_INCLUDE;
}
else {
return SKIP_BODY;
}
}
@Override
public int doAfterBody() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
System.out.println("-------------doEndTag-------------");
return EVAL_BODY_AGAIN;
}
1、继承助手类
public class OutTag extends BodyTagSupport {
}
2、开始编码
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
1、继承助手类
同Out可得,与Out的原理一样
2、开始编码
同Out可得,与Out的原理一样、代码及其类似
1、继承助手类
/**
* 1、值的传递 id、name
* 2、数据源 items
* 3、展示列与数据存储列与实体类的对应关系 textKey textVal
* 4、数据回显 selectedVal
* 5、下拉框可能有默认值 headerTextKey headerTextVal
*
*
*
*
*/
public class SelectTag extends BodyTagSupport{
}
2、开始编码
private String id;
private String name;
private List items=new ArrayList<>();
private String textKey; //id=textKey
private String textVal;
private String selectedVal;
private String headerTextKey;
private String headerTextVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
try {
out.print(toHTML());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
//
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
StringBuffer sb=new StringBuffer();
sb.append("
1、继承助手类
public class TestTag extends BodyTagSupport{
}
2、开始编码
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TestTag() {
super();
}
public TestTag(String name) {
super();
this.name = name;
}
public static void main(String[] args) {
}
/**
* doStartTag 执行到开始标签时执行的动作
* 执行到开始标签位置时对应执行的动作 doStartTag
* 2个返回值
* SKIP_BODY 跳过主体内容
* EVAL_BODY_INCLUDE 计算页面主体内同并包含再输出
*
*/
@Override
public int doStartTag() throws JspException {
System.out.println("-------------doStartTag-------------");
return EVAL_BODY_INCLUDE;
}
/**
* doAfterBody
* 介于内容 之间执行的动作
* 2个返回值
* EVAL_BODY_AGAIN 再次计算主体内容并输出
* SKIP_BODY 跳过主体内容
*
*/
@Override
public int doAfterBody() throws JspException {
System.out.println("-------------doAfterBody-------------");
return SKIP_BODY;
}
/**
* doEndTag 执行到结束标签时执行的动作
* 执行结束标签位置时对应执行的动作 doEndTag
* 2个返回值
* SKIP_PAGE 跳过页面的后续内容
* EVAL_PAGE 计算页面的后续内容
*
*
*/
@Override
public int doEndTag() throws JspException {
System.out.println("-------------doEndTag---------------");
return SKIP_PAGE;
}
1、继承助手类
public class CheckboxTag extends BodyTagSupport {
}
2、开始编码
private String textKey;
private String textVal;
private List checkedVal = new ArrayList<>();
private List item = new ArrayList<>();
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public List getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List checkedVal) {
this.checkedVal = checkedVal;
}
public List getItem() {
return item;
}
public void setItem(List item) {
this.item = item;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
StringBuffer sb = new StringBuffer();
String value;
String html;
for (Object obj : item) {
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
value = (String) field.get(obj);
Field field2 = obj.getClass().getDeclaredField(textVal);
field2.setAccessible(true);
html = (String) field2.get(obj);
if(checkedVal.contains(value)) {
sb.append(""+html+"");
}else {
sb.append(""+html+"");
}
}
return sb.toString();
}
wyy 1.1 core library
wyy core
1.1
w
/wyy
test
com.wyy.taglib.TestTag
JSP
name
true
false
if
com.wyy.taglib.IfTag
JSP
test
true
true
out
com.wyy.taglib.OutTag
JSP
value
true
true
foreach
com.wyy.taglib.ForeachTag
JSP
item
true
true
var
true
false
select
com.wyy.taglib.SelectTag
JSP
id
false
false
name
false
false
items
true
true
textKey
true
false
textVal
true
false
selectedVal
false
true
headerTextKey
false
false
headerTextVal
false
false
checkbox
com.wyy.taglib.CheckboxTag
JSP
textKey
true
true
textVal
true
true
item
true
true
checkedVal
false
true
<%
List ls=new ArrayList();
ls.add(new Student("001","aaaa"));
ls.add(new Student("002","bbbb"));
ls.add(new Student("003","cccc"));
request.setAttribute("ls", ls);
List ls2=new ArrayList();
ls2.add(new Student("001","aaaa"));
ls2.add(new Student("002","bbbb"));
ls2.add(new Student("003","cccc"));
request.setAttribute("ls2", ls2);
%>