第一步:webroot/web-inf下简历str.tld文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 3 <taglib> <!-- 自定义库标签的根 --> 4 <tlibversion>1.2</tlibversion> <!-- 版本号 --> 5 <jspversion>1.1</jspversion> <!-- JSP版本号 --> 6 <shortname>stu</shortname> <!-- prefix="stu"标签名称--> 7 <uri>StudentTags</uri> <!-- uri="StudentTags" 外界导入标签时,认识的名字,很重要。--> 8 9 <tag> 10 <name>selectAll</name> <!-- 标签名称 --> 11 <tagclass>com.bjsxt.sxf.tag.SeclectAllSt</tagclass> <!-- 对应的java类的全路径 --> 12 </tag> 13 14 <!-- 有属性的标签,可以有多个属性 <attribute>并列 --> 15 <tag> 16 <name>selectBySex</name> <!-- 标签名称 --> 17 <tagclass>com.bjsxt.sxf.tag.SeclectStBySex</tagclass> 18 <attribute> <!-- 对应标签的属性。 --> 19 <name>sex</name> 20 <required>true</required> <!-- 是必需写的属性,即没有写属性标签不能被正常使用 --> 21 </attribute> 22 </tag> 23 <!-- 按班级学生id查询出班级集合,存放到属性var指定的变量中,然后利用s标签的迭代标签,将var指定的变量存放的学生集合遍历出来 --> 24 <tag> 25 <name>selectByClassId</name> <!-- 标签名称 --> 26 <tagclass>com.bjsxt.sxf.tag.MyTag</tagclass> <!-- 对应的java类的全路径 --> 27 <body-content>JSP</body-content><!-- 如果不需要标签体则设置empty,反之设定jsp,内部可以运行jsp所有的语法 --> 28 <attribute> 29 <name>classId</name><!--指定属性名 和标签java类一致--> 30 <required>true</required><!--该属性是否是必须,如果非必须没设置则为空。 --> 31 <rtexprvalue>true</rtexprvalue><!-- 该属性能不能动态使用表达式为该属性赋值 true可以 false不可以 使用脚本和EL表达式来获取动态的值 --> 32 </attribute> 33 <attribute> 34 <name>var</name> 35 <required>true</required> 36 <rtexprvalue>false</rtexprvalue> 37 </attribute> 38 <attribute> 39 <name>num</name> 40 <required>false</required> 41 <rtexprvalue>false</rtexprvalue> 42 </attribute> 43 </tag> 44 <!-- 自定义的myif标签 --> 45 <tag> 46 <name>myif</name> 47 <tagclass>com.bjsxt.sxf.tag.MyIfTag</tagclass> 48 <bodycontent>JSP</bodycontent> 49 <attribute> 50 <name>flag</name> 51 <required>true</required> 52 <rtexprvalue>true</rtexprvalue> 53 </attribute> 54 <attribute> 55 <name>condition</name> 56 <required>true</required> 57 <rtexprvalue>true</rtexprvalue> 58 </attribute> 59 </tag> 60 <!-- 自定义的myelseif标签 --> 61 <tag> 62 <name>myelseif</name> 63 <tagclass>com.bjsxt.sxf.tag.MyElseIfTag</tagclass> 64 <bodycontent>JSP</bodycontent> 65 <attribute> 66 <name>flag</name> 67 <required>true</required> 68 <rtexprvalue>true</rtexprvalue> 69 </attribute> 70 <attribute> 71 <name>condition</name> 72 <required>true</required> 73 <rtexprvalue>true</rtexprvalue> 74 </attribute> 75 </tag> 76 <!-- 自定义的myelse标签 --> 77 <tag> 78 <name>myelse</name> 79 <tagclass>com.bjsxt.sxf.tag.MyElseTag</tagclass> 80 <bodycontent>JSP</bodycontent> 81 </tag> 82 83 </taglib>
第二步:建立标签类
1 package com.bjsxt.sxf.tag; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.apache.struts2.components.Component; 7 import org.apache.struts2.views.jsp.ComponentTagSupport; 8 9 import com.bjsxt.sxf.tag.compant.MyIfComponent; 10 import com.opensymphony.xwork2.util.ValueStack; 11 /** 12 * myif的标签类 13 * @ClassName: MyIfTag 14 * @Description: TODO(这里用一句话描述这个类的作用) 15 * @author 尚晓飞 16 * @date 2014-10-22 下午3:46:45 17 * 18 */ 19 public class MyIfTag extends ComponentTagSupport { 20 private String flag;//if判断的标示 21 private String condition;//可以传递的多个参数代表 22 23 //生成标签逻辑类的对象。多态。父类引用,指向子类对象 24 @Override 25 public Component getBean(ValueStack arg0, HttpServletRequest arg1, 26 HttpServletResponse arg2) { 27 return new MyIfComponent(arg0, arg1,arg2); 28 } 29 30 //给逻辑类的属性赋值 31 @Override 32 protected void populateParams() { 33 super.populateParams(); 34 MyIfComponent myIfComponent=(MyIfComponent) getComponent(); 35 myIfComponent.setFlag(flag); 36 myIfComponent.setCondition(condition); 37 } 38 39 public String getFlag() { 40 return flag; 41 } 42 43 public void setFlag(String flag) { 44 this.flag = flag; 45 } 46 47 public String getCondition() { 48 return condition; 49 } 50 51 public void setCondition(String condition) { 52 this.condition = condition; 53 } 54 55 56 } 57 58 59 package com.bjsxt.sxf.tag; 60 61 import javax.servlet.http.HttpServletRequest; 62 import javax.servlet.http.HttpServletResponse; 63 64 import org.apache.struts2.components.Component; 65 import org.apache.struts2.views.jsp.ComponentTagSupport; 66 67 import com.bjsxt.sxf.tag.compant.MyElseIfComponent; 68 import com.opensymphony.xwork2.util.ValueStack; 69 /** 70 * myelseif的标签类 71 * @ClassName: MyElseIfTag 72 * @Description: TODO(这里用一句话描述这个类的作用) 73 * @author 尚晓飞 74 * @date 2014-10-22 下午3:47:59 75 * 76 */ 77 public class MyElseIfTag extends ComponentTagSupport{ 78 private String flag;//elsif判断的标示 79 private String condition;//可以传递的多个参数代表 80 81 82 //返回该标签逻辑类的对象 83 @Override 84 public Component getBean(ValueStack arg0, HttpServletRequest arg1, 85 HttpServletResponse arg2) { 86 87 return new MyElseIfComponent(arg0, arg1, arg2); 88 } 89 90 //给该标签逻辑类对象赋值 91 @Override 92 protected void populateParams() { 93 super.populateParams(); 94 MyElseIfComponent myElseIfComponent=(MyElseIfComponent) getComponent(); 95 myElseIfComponent.setFlag(flag); 96 myElseIfComponent.setCondition(condition); 97 } 98 public String getFlag() { 99 return flag; 100 } 101 public void setFlag(String flag) { 102 this.flag = flag; 103 } 104 public String getCondition() { 105 return condition; 106 } 107 public void setCondition(String condition) { 108 this.condition = condition; 109 } 110 111 112 } 113 114 115 package com.bjsxt.sxf.tag; 116 117 import javax.servlet.http.HttpServletRequest; 118 import javax.servlet.http.HttpServletResponse; 119 120 import org.apache.struts2.components.Component; 121 import org.apache.struts2.views.jsp.ComponentTagSupport; 122 123 import com.bjsxt.sxf.tag.compant.MyElseComponent; 124 import com.opensymphony.xwork2.util.ValueStack; 125 /** 126 * 自定义的else标签 127 * @ClassName: MyElse 128 * @Description: TODO(这里用一句话描述这个类的作用) 129 * @author 尚晓飞 130 * @date 2014-10-22 下午2:24:30 131 * 132 */ 133 public class MyElseTag extends ComponentTagSupport{ 134 135 @Override 136 public Component getBean(ValueStack arg0, HttpServletRequest arg1, 137 HttpServletResponse arg2) { 138 // TODO Auto-generated method stub 139 return new MyElseComponent(arg0, arg1, arg2); 140 } 141 142 @Override 143 protected void populateParams() { 144 // TODO Auto-generated method stub 145 super.populateParams(); 146 } 147 148 149 }
第三步:建立标签逻辑类
1 package com.bjsxt.sxf.tag.compant; 2 3 import java.io.Writer; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts2.components.Component; 9 10 import com.opensymphony.xwork2.util.ValueStack; 11 12 /** 13 * myif标签逻辑类 没有@的标签注解,好像不影响, 14 * @ClassName: MyIfComponent 15 * @Description: TODO(这里用一句话描述这个类的作用) 16 * @author 尚晓飞 17 * @date 2014-10-22 下午3:49:40 18 * 19 */ 20 public class MyIfComponent extends Component { 21 protected String flag; 22 protected String condition; 23 public static final String MyANSWER = "struts.if.answer"; 24 protected Boolean answer; 25 26 27 public MyIfComponent(ValueStack stack,HttpServletRequest req,HttpServletResponse resp) { 28 super(stack); 29 } 30 31 32 //标签运行开始执行 33 @Override 34 public boolean start(Writer writer) { 35 answer = (Boolean) findValue(flag, Boolean.class); 36 if (answer == null) { 37 answer = Boolean.FALSE; 38 } 39 40 stack.getContext().put(MyANSWER, answer); 41 boolean flg=answer.booleanValue(); 42 return flg; 43 } 44 45 //标签运行结束执行 46 @Override 47 public boolean end(Writer writer, String body) { 48 stack.getContext().put(MyANSWER, answer); 49 boolean fd=super.end(writer, body); 50 return fd; 51 } 52 public String getFlag() { 53 return flag; 54 } 55 56 public void setFlag(String flag) { 57 this.flag = flag; 58 } 59 60 public String getCondition() { 61 return condition; 62 } 63 64 public void setCondition(String condition) { 65 this.condition = condition; 66 } 67 68 69 } 70 71 72 package com.bjsxt.sxf.tag.compant; 73 74 import java.io.Writer; 75 import java.util.Map; 76 77 import javax.servlet.http.HttpServletRequest; 78 import javax.servlet.http.HttpServletResponse; 79 80 import org.apache.struts2.components.Component; 81 82 import com.opensymphony.xwork2.util.ValueStack; 83 84 /** 85 * myelseif的逻辑类 86 * @ClassName: MyElseIfComponent 87 * @Description: TODO(这里用一句话描述这个类的作用) 88 * @author 尚晓飞 89 * @date 2014-10-22 下午3:50:49 90 * 91 */ 92 public class MyElseIfComponent extends Component { 93 protected String flag; 94 protected String condition; 95 public static final String MyANSWER = "struts.myelseif.answer"; 96 protected Boolean answer; 97 98 public MyElseIfComponent(ValueStack stack,HttpServletRequest req,HttpServletResponse resp) { 99 super(stack); 100 // TODO Auto-generated constructor stub 101 } 102 103 104 @Override 105 public boolean start(Writer writer) { 106 boolean myflag=false; 107 //获取值栈 108 Map context = stack.getContext(); 109 //先从值栈中获取if标签的flag的值 110 Boolean ifResult = (Boolean) context.get(MyIfComponent.MyANSWER); 111 //如果存在elseif 则需要获取该值 112 Boolean elseIfResult=(Boolean) context.get(MyElseIfComponent.MyANSWER); 113 context.remove(MyIfComponent.MyANSWER); 114 context.remove(MyElseIfComponent.MyANSWER); 115 116 //判断是否存在if=true的情况 117 if(ifResult!=null&&ifResult==true){ 118 //if=true 119 myflag=false; 120 }else{ 121 //if=false 122 //是否存在else if 123 if(elseIfResult!=null&&elseIfResult==true){ 124 //else if=true; 125 myflag=false; 126 }else{ 127 //else if=false或者不存在上个else if 128 answer = (Boolean) findValue(flag, Boolean.class); 129 if (answer == null) { 130 answer = Boolean.FALSE; 131 } 132 stack.getContext().put(MyANSWER, answer); 133 myflag=answer.booleanValue(); 134 } 135 } 136 return myflag; 137 } 138 @Override 139 public boolean end(Writer writer, String body) { 140 stack.getContext().put(MyANSWER, answer); 141 return super.end(writer, body); 142 } 143 public String getFlag() { 144 return flag; 145 } 146 public void setFlag(String flag) { 147 this.flag = flag; 148 } 149 public String getCondition() { 150 return condition; 151 } 152 public void setCondition(String condition) { 153 this.condition = condition; 154 } 155 public Boolean getAnswer() { 156 return answer; 157 } 158 public void setAnswer(Boolean answer) { 159 this.answer = answer; 160 } 161 162 163 } 164 165 166 package com.bjsxt.sxf.tag.compant; 167 168 import java.io.Writer; 169 import java.util.Map; 170 171 import javax.servlet.http.HttpServletRequest; 172 import javax.servlet.http.HttpServletResponse; 173 174 import org.apache.struts2.components.Component; 175 176 import com.opensymphony.xwork2.util.ValueStack; 177 178 public class MyElseComponent extends Component{ 179 180 /** 181 * myelse的逻辑类 182 * 183 * @Title: EncodingRequestWrapper 184 * @Description: 构造函数 185 * @param stack 186 * @param req 187 * @param response 188 * @author 尚晓飞 189 * @date 2014-10-22 下午3:51:24 190 */ 191 public MyElseComponent(ValueStack stack,HttpServletRequest req,HttpServletResponse response) { 192 super(stack); 193 } 194 //Myelse标签运行开始 195 @Override 196 public boolean start(Writer writer) { 197 boolean myflg=false; 198 //获取值栈 199 Map context = stack.getContext(); 200 //获取if的值 201 Boolean ifResult = (Boolean) context.get(MyIfComponent.MyANSWER); 202 //获取else的值 203 Boolean elseifResult=(Boolean) context.get(MyElseIfComponent.MyANSWER); 204 //移除值栈中的值 205 context.remove(MyIfComponent.MyANSWER); 206 context.remove(MyElseIfComponent.MyANSWER); 207 208 //判断是否存在if 209 if(ifResult!=null&&ifResult==true){ 210 //存在if,并且为true; 211 myflg=false; 212 }else{ 213 //if为false 214 //判断elseif是否存在 215 if(elseifResult!=null&&elseifResult==true){ 216 //存在elseif标签,且==true 217 myflg=false; 218 }else{ 219 //不存在elseif标签或者为elseif标签==false或者if标签为false 220 myflg=true; 221 } 222 } 223 224 225 return myflg; 226 } 227 228 @Override 229 public boolean end(Writer writer, String body) { 230 // TODO Auto-generated method stub 231 boolean d=super.end(writer, body); 232 return d; 233 } 234 235 236 237 238 }
第四步:测试页面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="StudentTags" prefix="stu" %> 3 <%@ taglib uri="/struts-tags" prefix="s"%> 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 </head> 13 14 <body> 15 This is my JSP page. <br> 16 <a href="<%=request.getContextPath()%>/StudentAction!reportStudent.action">班级表单下载</a> 17 <h1>查询出所有学生的标签效果</h1> 18 <stu:selectAll></stu:selectAll> 19 <h1>查询出指定性别的学生的标签效果</h1> 20 <stu:selectBySex sex="男"></stu:selectBySex> 21 <h1>查询出指定班级id的学生集合的标签效果</h1> 22 <stu:selectByClassId var="students" classId="1" > 23 <table border="2"> 24 <tr> 25 <td>id</td> 26 <td>姓名</td> 27 <td>性别</td> 28 <td>班级</td> 29 </tr> 30 <s:iterator value="#students" var="stu"> 31 <tr> 32 <td><s:property value="#stu.id"/></td> 33 <td><s:property value="#stu.name"/></td> 34 <td><s:property value="#stu.sex"/></td> 35 <td><s:property value="#stu.classRoom.name"/></td> 36 </tr> 37 </s:iterator> 38 </table> 39 </stu:selectByClassId> 40 41 <h1>我是中国人</h1> 42 <stu:myif flag="1==3" condition="shangxiaofei"> 43 <h1>myif是true</h1> 44 </stu:myif> 45 <stu:myelseif flag="1==2" condition="first"> 46 <h1>第一个myelseif是false</h1> 47 </stu:myelseif> 48 <stu:myelseif flag="1==1" condition="sencod"> 49 <h1>第二个myelseif是true</h1> 50 </stu:myelseif> 51 <stu:myelse> 52 <h1>false</h1> 53 </stu:myelse> 54 </body> 55 56 </html>
第五步:效果图