前言
基于jsp自定义标签我们上一篇已经讲解了一些基本的语法、属性、以及开发步骤和注意事项。
那么我们今天就开始来开发Select标签(自动选中)
开发步骤:
- 定义助手类
- 配置tld文件对应的属性以及约束
- 引入自定义标签库的路径
常用作用域来存取数据有四个:
- pageContext
- request
- session
- application
基于开发步骤后(select):
案例: z:deptList 、z:select(基于mytag.tld)
1.tld文件
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>Simple Tags</short-name>
<uri>/liyingdong</uri>
<!-- 自定义数据标签 -->
<tag>
<name>deptList</name>
<tag-class>com.liyingdong.DeptListTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- 自定义下拉框标签 -->
<tag>
<name>Select</name>
<tag-class>com.liyingdong.SelectTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssStyle</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssClass</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headText</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>valueProperty</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textProperty</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>selectValue</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.dao方法
3.实体类
package com.liyingdong.entity;
public class Dept {
private String deptId;//部门id
private String deptName;//部门姓名
public Dept() {}
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Dept(String deptId, String deptName) {
this.deptId = deptId;
this.deptName = deptName;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", deptName=" + deptName + "]";
}
}
4.数据标签z:deptList
package com.liyingdong;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.liyingdong.dao.DeptDao;
import com.liyingdong.entity.Dept;
public class DeptListTag extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String var;//保存数据到作用域的名字
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
DeptDao d=new DeptDao();
List<Dept> all = d.getAll();
System.out.println(all.toString());
if(all.size()!=0) {
pageContext.setAttribute(var,all);
return SKIP_BODY;
}
return SKIP_BODY;//跳过主体
}
}
5.Select标签
以下几个关键点:
- 反射赋值jar减少了代码量
- UI标签的核心点:
JspWriter out = pageContext.getOut();
代码:
package com.liyingdong;
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.BeanUtils;
public class SelectTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
private String name;//名字
private String valueProperty;//deptid:java属性
private String textProperty;//deptName:java属性
private String headText;//头文本
private String headValue;//头的值
private List<Object> items;//数据源
private String cssStyle;//样式
private String selectValue;//下拉框的value值
private String cssClass;//cs样式
private String var;//保存数据到一个key
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValueProperty() {
return valueProperty;
}
public void setValueProperty(String valueProperty) {
this.valueProperty = valueProperty;
}
public String getTextProperty() {
return textProperty;
}
public void setTextProperty(String textProperty) {
this.textProperty = textProperty;
}
public String getHeadText() {
return headText;
}
public void setHeadText(String headText) {
this.headText = headText;
}
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getCssStyle() {
return cssStyle;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
public String getSelectValue() {
return selectValue;
}
public void setSelectValue(String selectValue) {
this.selectValue = selectValue;
}
public String getCssClass() {
return cssClass;
}
public void setCssClass(String cssClass) {
this.cssClass = cssClass;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
/**
* 实现处理开始标签的方法
*/
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
//获取到out
JspWriter out = pageContext.getOut();
try {
//将下拉框输出到html上面显示
out.print(toHtml());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
/**
* 封装一个方法
*/
public String toHtml() throws Exception{
//定义StringBuffer拼接字符串
StringBuffer sb=new StringBuffer();
//拼接好头部样式
sb.append("<select name='"+this.name+"' style='"+this.cssStyle+"' class='"+this.cssClass+"'>");
sb.append("<option value='"+this.headValue+"' >"+this.headText+"</option>");
//判断数据源是否有值
if(null!=items) {
for (Object obj : items) {
//利用反射jar包中的方法进行反射赋值 分别拿到deptid和deptName 注意点:对应实体类的值一定要正确
String deptId = BeanUtils.getProperty(obj,valueProperty);
String deptName = BeanUtils.getProperty(obj,textProperty);
//如果指定的值和下拉框中的值一样那么就选中
if(deptId.equals(this.selectValue)) {
sb.append("<option selected value='"+deptId+"'>"+deptName+"</option>");
}
sb.append("<option value='"+deptId+"'>"+deptName+"</option>");
}
}
sb.append("</select>");
//返回拼接好的字符串
return sb.toString();
}
}
测试界面:
- 可以根据selectValue值进行下拉框的选中
- 如果值不匹配就默认请选择;
- 注意点:
- valueProperty要对应实体类的id
- textProperty要对应实体类的name
- 可以对内置select设置样式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="d" uri="/liyingdong"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
.c1{
font-size: 30px;
color:red;
}
</style>
</head>
<body>
<h1>数据标签</h1>
<d:deptList var="deptlist"></d:deptList>${deptlist }
<h1>下拉框标签</h1>
<d:Select name="SelectName" valueProperty="deptId" textProperty="deptName" headText="---请选择---" headValue="-1" items="${ deptlist}" cssStyle="width:400px" var="v" selectValue="5" cssClass="c1"/>
</body>
</html>
输出结果: