自定义标签
1. UI标签:out select checkbox
2. 控制标签 if forEach
3. 数据标签 set
1. UI标签:out select
有设置就有输出接下来让我们先写一下set 与out的的自定义标签
Set标签有var和value两个属性,Out标签只有value一个属性,所以我们先定义它们助手类
public class SetTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private String var;
private Object value;
@Override
public int doStartTag() throws JspException {
//内置对象
pageContext.setAttribute(var, value);
return SKIP_BODY;//跳过主体
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
public class OutTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;//防止报空指针异常
private Object value;//Objiec 打印的可能是任何类型
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out= pageContext.getOut();//输出流
try {
out.print(value.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
}
配置tld文件
<tag>
<name>set</name>
<tag-class>com.xyx.jsp2.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.xyx.jsp2.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
输出:
<z:set var="name" value="zs"></z:set>
<z:out value="${name }"></z:out>
***2. 控制标签 if ***
if标签只有一个属性值test
助手类:
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = -8477321984637500253L;
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;//三目运算符
}
}
配置tld文件:
<tag>
<name>if</name>
<tag-class>com.xyx.jsp2.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
输出:
<z:if test="true">lisi</z:if>//出现结果
<z:if test="false">wangwu</z:if>//不出想
2.1 控制标签 foreach
foreach 有两个属性值一个是var 一个是集合items
助手类:
public class ForeachTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String var;
private List<Object> items=new ArrayList<>();
/**
* 执行完这个的时候,var代表的指针一定要向下移动一位;
*/
@Override
public int doStartTag() throws JspException {
if(items.size()==0) {//如果items的长度大于0
return SKIP_BODY;//跳过主体
} else {
//pageContext只限于当前界面
Iterator<Object> it = items.iterator();//迭代器
pageContext.setAttribute(var, it.next());//第一个对象
pageContext.setAttribute("it", it);//把迭代器往下传 if=1时
return EVAL_BODY_AGAIN;//在循环一次
}
}
@Override
public int doAfterBody() throws JspException {
Iterator<Object> it=(Iterator<Object>)pageContext.getAttribute("it");//获得上面的迭代器
if(it.hasNext()){//迭代器继续循环 如果还有下一个
pageContext.setAttribute(var, it.next());//往下移
pageContext.setAttribute("it", it);
return EVAL_BODY_AGAIN;
}
return EVAL_PAGE;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
}
配置tld文件:
<tag>
<name>foreach</name>
<tag-class>com.xyx.jsp2.ForeachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
输出
<%
List list=new ArrayList();
list.add(new Student("001","xiaocheng"));
list.add(new Student("002","xiaohua"));
list.add(new Student("003","xiaolv"));
request.setAttribute("stus", list);
%>
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
注:在这里需要写一个学生实体类,这个实体类属性有学号与姓名
注:导包不要导错了
/**
* 1.值得传递 id,name
* 2.数据源 iteams
* 3.展示列与数据存储列与实体列的对应关系 textKey textVal
* 4.数据回显 selectedVal
* 5.可能下拉框默认值(投标签) headerTextKey headerTextVal
*
* @author XYX
*
*/
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String id;
private String name;
private List<Object> items=new ArrayList<>();
private String 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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
//用字符串拼接起来
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();//相当于String安全一些
sb.append("<select id='"+id+"' name='"+name+"'>");//拼接
if(!(headerTextKey==null|| "".equals(headerTextKey) ||
headerTextVal==null||"".equals(headerTextVal))) {
sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value;//存到数据库里面的
String html;//内容
for (Object obj : items) {
//<option value=''></option>
//反射 第一种方式
Field field=obj.getClass().getDeclaredField(textKey);//从前台传来 获取它的属性名
field.setAccessible(true);//打开访问权限
value=(String) field.get(obj);
//第一种方式
html=(String) PropertyUtils.getProperty(obj, textVal);//需要导包 与上面的代码意思一致
if(value.equals(selectedVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option value='"+value+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
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 String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
}
配置tld文件
<tag>
<name>select</name>
<tag-class>com.xyx.jsp2.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
输出:
`<%
List list=new ArrayList();
list.add(new Student("001","xiaocheng"));
list.add(new Student("002","xiaohua"));
list.add(new Student("003","xiaolv"));
request.setAttribute("stus", list);
%>
<s:select headerTextKey="-1" headerTextVal="=====请选择=====" textVal="name" items="${stus }"`
page(pageContext)|request(…)|session(…)|application(…)
存储和交换数据
checkbox
首先让我们先写助手类
public class CheckedTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private String textKey;//传入值
private String textVal;//显示值
private List<Object> checkedVal=new ArrayList<>();//数据回显用于默认选中
private List<Object> 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<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
public CheckedTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
this.item = item;
}
public CheckedTag() {
super();
}
//以上是实体类
@Override
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();//获取out
try {
try {
out.print(toHTML());//再下面创建一个方法
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {//记得要把数据类型改为String
StringBuffer sb=new StringBuffer();//比String安全
String value;//存到数据库里面的
String html;//内容
for (Object obj : item) {//遍历
Field textKeyfield = obj.getClass().getDeclaredField(textKey);//获得属性值
textKeyfield.setAccessible(true);//打开访问权限
value=(String)textKeyfield.get(obj);//获取
Field textValfield = obj.getClass().getDeclaredField(textVal);
textValfield.setAccessible(true);
html=(String)textValfield.get(obj);
//html=(String) PropertyUtils.getProperty(obj, textVal);//需要导包 与上面的代码意思一致
if(checkedVal.contains(value)) {//判断数据回显是否包含value 包含的化就默认
sb.append("<input checked type='checkbox' value='"+value+"'/>"+html+"");
}else {
sb.append("<input type='checkbox' value='"+value+"'/>"+html+"");
}
}
return sb.toString();//返回值要特别注意
}
}
配置tld文件:
<tag>
<name>checkbox</name>
<tag-class>com.zking.jsp.day02.CheckedTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>item</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>checkedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
//输出
<%
List ls=new ArrayList();
ls.add(new Student("001","xiaocheng"));
ls.add(new Student("002","xiaohua"));
ls.add(new Student("003","xiaolv"));
List lis=new ArrayList();
lis.add("001");
lis.add("002");
lis.add("003");
request.setAttribute("ls", ls);
request.setAttribute("lis", lis);
%>
<z:checkbox textVal="name" checkedVal="${lis}" item="${ls}" textKey="id"></z:checkbox>
结果如图:


被折叠的 条评论
为什么被折叠?



