JSP自定义select标签中取值

JSP自定义select标签的取值

select取值是通过name属性获取。
自定义标签的流程:

1.继承JSP标签的标签类TagSupport,SimpleTagSupport等,重写dostart()或doTag()等方法;

			public class DictTag extends TagSupport{
	
			private static Log log = LogFactory.getLog(DictTag.class);
			private String dictType;
			//设置默认值
			private String selectValue;
			private String id;
			private String name;
			private String htmlType;
			private static final long serialVersionUID = 1L;

			@Override
			public int doStartTag() throws JspException {
			try {
				String str = "";
				if("select".equals(htmlType)){
					str = genSelect();
				}
				log.info(str);
				pageContext.getOut().write(str);

			} catch (Exception e) {
			
			}
			   return EVAL_BODY_INCLUDE;
			}

 
   

2.定义自定义标签的属性生成get,set方法

		private String genSelect(){
			/**/
			//selected="selected"
			StringBuffer sb = new StringBuffer();
			/*sb.append(""); 
    List ds = SystemCache.getDictList(dictType); 
    for(Dict d:ds){ 
    String sd = ""; 
    if(d.getCode().equals(selectValue)){sd = "selected=\"selected\""; 
    }sb.append(""); 
    } 
    return sb.toString(); 
    } 
   
 
   
3.后台获取数据的方法业务的实现

4.编写.tld配置文件

			1.0
			1.2
			http://www.crm.com/dict/tag
			自定义标签
			
			
				dictTag
				com.xyy.springmvc.mybatis.tag.DictTag
				//标签属性定义
					id
					false
					true
					java.lang.String
				
			
			
JSP自定义select标签中取值_第1张图片

5.JSP页面引用标签

			<%@ taglib uri="/WEB-INF/tld/dict.tld" prefix="dic" %>
		
		 

 
   


你可能感兴趣的:(前端)