1.set和out标签
set的一个助手类
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() { pageContext.setAttribute(var, value);//将值存入pageContext return SKIP_BODY; }
set的tld文件
<tag> <name>set</name> <tag-class>com.jsp.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的助手类
public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } private Object value; @Override public int doStartTag() throws JspException { JspWriter out=pageContext.getOut(); try { out.write(value.toString()); } catch (IOException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } return SKIP_BODY; }
out的tld文件
<tag> <name>out</name> <tag-class>com.jsp.OutTag</tag-class> <body-content>empty</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
下面我通过jsp文件做的演示
<p:set value="zhangsan" var="name"></p:set> <p:out value="${name }"/>
结果
if标签助手类
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 SKIP_BODY;// 不显示标签中间的内容 } return EVAL_BODY_INCLUDE;// 显示标签中间的内容 }
if标签的tld文件
<tag> <name>if</name> <tag-class>com.jsp.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
if标签测试
<p:if test="true">ls</p:if> <p:if test="false">ww</p:if>
test里面为true会显示在jsp页面上,false的不显示
foreach标签的助手类
private static final long serialVersionUID = 2893253295092920387L; //集合 private List<Object> items; //变量的名称 private String var; public List<Object> getItems() { return items; } public void setItems(List<Object> items) { this.items = items; } public String getVar() { return var; } public void setVar(String var) { this.var = var; } @Override public int doStartTag() throws JspException { 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; }else { return EVAL_PAGE; } }
foreach的tld文件
<tag> <name>foreach</name> <tag-class>com.jsp.ForEachTag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>true</required> <rtexprvalue>flase</rtexprvalue> </attribute> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
foreach标签的测试
<% List list=new ArrayList(); list.add(new Student("sb1","zs")); list.add(new Student("sb2","ls")); list.add(new Student("sb3","ws")); request.setAttribute("stus",list); %> <p:foreach items="${stus }" var="stu"> ${stu.sid },${stu.sname }<br> </p:foreach>
结果是3个值全都显示出来
select标签助手类
private static final long serialVersionUID = 1L; private String id; private String name; private List<Object> items = new ArrayList<Object>(); private String textKey; private String textval; private String headTextKey; private String headTextVal; private String selectedval; 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 getHeadTextKey() { return headTextKey; } public void setHeadTextKey(String headTextKey) { this.headTextKey = headTextKey; } public String getHeadTextVal() { return headTextVal; } public void setHeadTextVal(String headTextVal) { this.headTextVal = headTextVal; } public String getSelectedval() { return selectedval; } public void setSelectedval(String selectedval) { this.selectedval = selectedval; } @Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.write(toHTML()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return super.doStartTag(); } /** * 拼接出下拉列表所对应的select的html代码 * <select id='' name=''> * <option value='-1'selected>===请选择===</option> * </select> * * @return * @throws IllegalAccessException * @throws Exception */ private String toHTML() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("<select id='" + id + "' name='" + name + "'>"); if (headTextKey == null || "".equals(headTextKey) || headTextVal == null || "".equals(headTextVal)) { sb.append("<option value='" + headTextKey + "' selected>" + headTextVal + "</option>"); } String val; String html; for (Object obj : items) { // 要让学生的ID存入数据库,让学生的名字展示在JSP页面 // <option value='s001'>ls</option> // obj sid sname Field textKeyField = obj.getClass().getDeclaredField(textKey); textKeyField.setAccessible(true); val = (String) textKeyField.get(obj); html = BeanUtils.getProperty(obj, textval); if(val.equals(selectedval)) { sb.append(" <option value='"+val+"'selected>"+html+"</option>"); }else { sb.append("<option value='"+val+"'>"+html+"</option>"); } } sb.append("</select>"); return sb.toString(); }
select的tld文件
<tag> <name>select</name> <tag-class>com.jsp.ServletTag</tag-class> <body-content>JSP</body-content> <attribute> <name>id</name> <required>false</required> <rtexprvalue>flase</rtexprvalue> </attribute> <attribute> <name>name</name> <required>flase</required> <rtexprvalue>flase</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>flase</rtexprvalue> </attribute> <attribute> <name>headTextKey</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>headTextVal</name> <required>false</required> <rtexprvalue>flase</rtexprvalue> </attribute> <attribute> <name>selectedval</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
select的测试
<p:select items="${stus }" textval="sname" textKey="sid"></p:select>
<p:select headTextKey="-1" headTextVal="===请选择学生===" items="${stus }" textval="sname" textKey="sid"></p:select>
第一种显示的
第二种的默认显示的第一个为你输入的值,今天的技术分享就到这里