1、set标签与out标签
set助手类
package com.zyp.jsp02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private String var;
private Object value;
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;
}
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);//将值存入pageContext
return SKIP_BODY;
}
}
配置set属性文件
<tag>
<name>set</name>
<tag-class>com.zyp.jsp02.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
out的助手类
package com.zyp.jsp02;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutTag extends BodyTagSupport{
private static final long serialVersionUID = 8655118409172739205L;
private Object value;
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);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
}
out配置文件
<tag>
<name>out</name>
<tag-class>com.zyp.jsp02.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
set\out测试结果
<z:set var="name" value="hello"></z:set>
<z:out value="${name }"></z:out>
结果是hello。
2、if标签
IF助手类【if只有test一个属性值】
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
//如果test为true,则进入doAfterBody方法,如果为false,则直接进入doEndTag方法
return test?EVAL_BODY_INCLUDE:SKIP_BODY;
}
IF配置属性文件
<tag>
<name>if</name>
<tag-class>com.zyp.jsp02.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>text</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
IF测试结果
<z:if text="true">aaa</z:if>
<z:if text="false">bbb</z:if>
输出结果是aaa出来,bbb则不会出来。
3、foreach标签
foreach助手类,虽然set,get的方法没有截取到,但并受不影响。
package com.zyp.jsp02;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForeachTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private String var;
private List<Object> items=new ArrayList<>();
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;
}
public int doStartTag() throws JspException {
if(items.size()==0) {
return SKIP_BODY;
}
else {
Iterator<Object> it = items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
}
@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;
}
}
配置Foreach属性文件
<tag>
<name>foreach</name>
<tag-class>com.zyp.jsp02.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>
Foreach测试结果
<%
List li=new ArrayList();
li.add(new Student("1","a"));
li.add(new Student("2","b"));
li.add(new Student("3","c"));
request.setAttribute("stus", li);
%>
<s:foreach items="${stus }" var="stu">
${stu.id }:${stu.name }<br>
</s:foreach>
输出结果: 1,a
2,b
3,c
4、select标签
select助手类
package com.zyp.jsp02;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.PropertyUtils;
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 {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb=new StringBuffer();
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) {
// 第一种方式 反射
// Field textKeyField = obj.getClass().getDeclaredField(textKey);
// textKeyField.setAccessible(true);
// value=(String) textKeyField.get(obj);
// 第二种方式 导jar包
value=(String) PropertyUtils.getProperty(obj, textKey);
html=(String) PropertyUtils.getProperty(obj, textVal);
if(value.equals(selectedVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}
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;
}
}
配置Select属性文件
<tag>
<name>select</name>
<tag-class>com.zyp.jsp02.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>
Select测试结果
<%
List li=new ArrayList();
li.add(new Student("1","a"));
li.add(new Student("2","b"));
li.add(new Student("3","c"));
request.setAttribute("stus", li);
%>
<s:select headerTextKey="-1" headerTextVal="请选择" textVal="name" items="${stus }" selectedVal="2" textKey="id"></s:select>
如果不想要选中的值就可以不要selectedVal的属性,如果没有默认选中值的时候就可以不要headerTextKey和headerTextVal这两个属性就可以了。
5、checkbox标签
<tag>
<name>checkbox</name>
<tag-class>com.zyp.jsp02.checkboxTag</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>
Checkebox助手类
public class CheckboxTag extends BodyTagSupport {
private String textKey;//传入值
private String textVal;//显示值
private List<Object> checkedVal=new ArrayList<>();//回显数据集合
private List<Object> item=new ArrayList<>();//数据集合
public List<Object> getItem() {
return item;
}
public void setItem(List<Object> item) {
this.item = item;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTextKey() {
return textKey;
}
public List<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public CheckboxTag() {
super();
}
public CheckboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
this.item = item;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException 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 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);
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();
}
}
Checkebox测试结果
<%
List ls=new ArrayList();
ls.add(new Student("1","aaa"));
ls.add(new Student("2","bbb"));
ls.add(new Student("3","ccc"));
List ls1=new ArrayList();
ls1.add("1");
ls1.add("2");
request.setAttribute("ls", ls);
request.setAttribute("ls1", ls1);
%>
<z:checkbox textVal="name" checkedVal="${ls1}" item="${ls}" textKey="id"></z:checkbox>
输出结果是aaa和bbb选中,ccc没有选中。