UI标签(用户界面的数据控制)
out输出
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//流对象 拿到输入对象
try {
out.print(value.toString());//拿到value对象
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
注1:JspWriter writer = pageContext.getOut();
select下拉框
/**
* 1、值的传递 id、name
* 2、数据源 items
* 3、展示列与数据存储列与实体列的对应关系 textKey textVal
* 4、数据回显 selectedVal
* 5、可能下拉框默认值(头标签) headerTextKey headerTextVal
* @author tt
* */
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;//textKey = id
private String textVal;
private String selectedVal;
private String headerTextVal;
private String headerTextKey;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//流对象
try {
out.print(toHTML());//调方法
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception {
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);
html = (String) PropertyUtils.getProperty(obj, textKey);
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 SelectTag() {
super();
}
控制标签(业务逻辑处理时的数据控制)
if
private boolean test;//属性
@Override
public int doStartTag() throws JspException {
return test ?EVAL_BODY_INCLUDE: SKIP_BODY;如果test为true则为输出,如果test为false则为跳过主体。
}
forEach
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) {
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 super.doAfterBody();
}
注1:page(pageContext)|request(…)|session(…)|application(…)
存储和交换数据
数据标签
数据标签就是用来存储数据和设置数据值的
set
private String var;
private String value;
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);//存储值
return SKIP_BODY;//跳过主体
}
建立z标签语言
<description>zking 1.1 core library</description>
<display-name>zking core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/zking</uri>//在jsp界面要调用的路径
<tag>
<name>set</name>
<tag-class>com.zking.jsp.day2.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>
<name>out</name>
<tag-class>com.zking.jsp.day2.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
............
在页面调用
建立一个jsp文件,在文件开头调用z语言
<%@ taglib prefix=“z” uri="/zking" %>
<body>
<z:set var="name" value="zhangsan"></z:set>
<z:out value="${name }"></z:out>
<z:if test="true">lisi</z:if>
<z:if test="false">wanwu</z:if>//在用户界面wanwu不会展示出来,因为test的属性为false
<%
List list = new ArrayList();
list.add(new Student("001","xiaocheng"));
list.add(new Student("002","xiaoli"));
list.add(new Student("003","xiaowang"));
request.setAttribute("stus", list);
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
<z:select selectedVal="002" headerTextKey="-1" headerTextVal="===请选择===" textVal="name" items="${stus }" textKey="id"></z:select>
</body>
%>
注意:一定要在写助手类的时候写构造方法。
不然会报:java.lang.NoSuchMethodException:com.TT.jsp.day1.DemoTag.()
数据字典
含义:对数据的数据项、数据结构、数据流、数据存储、处理逻辑等进行定义和描述,其目的是对数据流程图中的各个元素做出详细的说明,
作用:是一种用户可以访问的记录数据库和应用程序元数据的目录。方便对用户界面进行改动。有利于程序员人和分析员和用户的通信。是分析阶段的工具
Chackbox
public class Checkbox 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 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;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();//流对象
try {
out.print(toHTML());//调方法
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
public 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)) {
sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
}
else {
sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
}
}
return sb.toString();
}
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 Checkbox(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
super();
this.textKey = textKey;
this.textVal = textVal;
this.checkedVal = checkedVal;
this.item = item;
}
public Checkbox() {
super();
}
}
z语言定义
<tag>
<name>checkbox</name>
<tag-class>com.zking.jsp.day2.Checkbox</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</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>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
界面展示
<body>
<%
List list = new ArrayList();
list.add(new Student("1","xiaocheng","篮球"));
list.add(new Student("2","xiaocheng","滑冰"));
list.add(new Student("3","xiaocheng","看书"));
list.add(new Student("4","xiaocheng","听歌"));
List ls = new ArrayList();
ls.add("1");
ls.add("2");
request.setAttribute("stus", list);
request.setAttribute("ls", ls);
%>
<z:checkbox textVal="name" item="${stus }" textKey="id" checkedVal="${ls }"></z:checkbox>
</body>