欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP标签

程序员文章站 2022-06-08 18:39:53
...

JSP标签

2. UI标签
1.z:set
自定义一个set标签,out标签
1.set

private static final long serialVersionUID = 4965191755704898746L;
	
	private String var;
	private Object value;
	
	@Override
	public int doStartTag() throws JspException {
		pageContext.setAttribute(var, value);
//		HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();//开发权限标签需要用到
		return SKIP_BODY;
	}
	

	

private static final long serialVersionUID = 1L;

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.toString());
	} catch (IOException e) {
		e.printStackTrace();
	}
	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;
	}

在tld文件中设置


  <tag>
    <name>set</name>
    <tag-class>com.zking.jsp.day02.OutTag</tag-class>
    <body-content>JSP</body-content>
     <attribute>
        <name>var</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>out</name>
    <tag-class>com.zking.jsp.day02.OutTag</tag-class>
    <body-content>JSP</body-content>
     <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  

最后在jsp文件中实现

<z:set var="name" value="zhangsan"></z:set>
<z:out value="${name }"></z:out>

m:select 标签
1.比如说我们需要学生姓名,编号得下拉框
我们需要一个学生得实体类


public class Student {
	private String sid ;
	private String sname;
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Student(String sid, String sname) {
		super();
		this.sid = sid;
		this.sname = sname;
	}
	public Student() {
		super();
	}
	
	

然后我们新建一个select得类

/**
 * 1.值得传递 id,name
 * 2.数据源 items
 * 3.展示列与数据存储与实体类dui应关系  textKey textVal
 * 4.数据回显 selectedVal
 * 5.可能下拉框默认值  headerTextKey headerTextVal
 */

public class SelectTag extends BodyTagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5375902073989817712L;
	private String id;
	private String name;
	private List<Object> items = new ArrayList<Object>();
	private String textKey;//id
	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();
		sb.append("<select id='"+id+"' name='"+name+"'>");
		
		String value;
		String html;
		if(!(headerTextKey==null || "".equals(headerTextKey)||
				headerTextVal==null || "".equals(headerTextVal))) {
			sb.append("<option select value='"+headerTextKey+"'>"+headerTextVal+"</option>");
		}
		
		for (Object obj : items) {
			Field	textKeyField = obj.getClass().getDeclaredField(textKey);
			textKeyField.setAccessible(true);
			value = (String) textKeyField.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;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public SelectTag(String id, String name, List<Object> items, String textKey, String textVal, String selectedVal,
			String headerTextKey, String headerTextVal) {
		super();
		this.id = id;
		this.name = name;
		this.items = items;
		this.textKey = textKey;
		this.textVal = textVal;
		this.selectedVal = selectedVal;
		this.headerTextKey = headerTextKey;
		this.headerTextVal = headerTextVal;
	}
	public SelectTag() {
		super();
	}

然后我们需要在xml文件中实现select标签

 <tag>
    <name>select</name>
    <tag-class>com.zking.jsp.day02.SelectTag</tag-class>
    <body-content>JSP</body-content>
     <attribute>
        <name>id</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>

接着就可以在jsp文件中进行实现


<% 
	List ls = new ArrayList();
ls.add(new Student("001","xiaowang"));
ls.add(new Student("002","xiaoli"));
ls.add(new Student("003","xiaosan"));
request.setAttribute("stus", ls);


%>

<z:foreach items="${stus }" var="stu">
	${stu.id },${stu.sname }<br>

</z:foreach>

<z:select selectedVal="002" headerTextKey="-1" headerTextVal="===请选择===" textVal="name" items="${stus }" textKey="id"></z:select>

注1:JspWriter writer = pageContext.getOut();

3. 控制标签
实现标签得步骤基本上都是一样的
下面对if标签和foeach标签进行一个案例
我们首先需要一个学生的实体类
如上一个select标签
1. m:if

 */
	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 {
		// TODO Auto-generated method stub
		return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}
}

接着

 <tag>
    <name>if</name>
    <tag-class>com.zking.jsp.day02.IfTag</tag-class>
    <body-content>JSP</body-content>
     <attribute>
        <name>test</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

测试

<z:if test="true">zhangsan</z:if>
<z:if test="false">zhan</z:if>

2.m:forEach 3

/**
	 * 
	 */
	private static final long serialVersionUID = -1614598923980260048L;
	
	private String var;
	private List<Object> items = new ArrayList<>();
	
	@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()) {
			return EVAL_BODY_AGAIN;
		}
		return EVAL_PAGE;
	}

接着

  <tag>
    <name>foreach</name>
    <tag-class>com.zking.jsp.day02.Foreach</tag-class>
    <body-content>JSP</body-content>
     <attribute>
        <name>items</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
  

测试

<% 
	List ls = new ArrayList();
ls.add(new Student("001","xiaowang"));
ls.add(new Student("002","xiaoli"));
ls.add(new Student("003","xiaosan"));
request.setAttribute("stus", ls);


%>

<z:foreach items="${stus }" var="stu">
	${stu.id },${stu.sname }<br>

注1:page(pageContext)|request(…)|session(…)|application(…)
存储和交换数据