|
|
|
| 您的位置:首页>>JSP和Servlet>>一个表单域自动绑定的Tag实现方法 |
|
|
一个表单域自动绑定的Tag实现方法
|
| 2006-04-05 来源:ChinaITLab 作者:ChinaITLab |
写了这么长时间的web应用了,对流行的web框架中的tag也算是精通了,不过唯一能让我在所有项目中都毫不犹豫的使用的也就是jstl了。 一般对于没有美工人员介入的项目,当选定web框架后,我会使用此web框架的html标签库,但是一旦有美工人员参与,我一般都放弃使用这些框架中的html标签库,就拿strurs和webwork来说吧,我宁愿把表单域写成这样:<input type="text" name="username" value="<c:out value="${user.username}" /> >,也不愿意用<html:text property="username" />或者<ww:textfield name="username" />。尽管后者看起来很清爽,但是这对于一个和美工合作的项目来说,对自己简直是一种折磨。 毕竟美工不会去学习这么多的标记,他们只知道原生的html,因此,我个人认为只要有美工参与,最好少用框架的html标签库,尽量不要过多的侵入原生的html。 记得以前写过一个基于webwork的formtag,为的就是减少对html的侵入,但是总感觉那个扩展性教差,所以一直也没在用了,现在又碰到了类似的问题,所以决定写一个FieldBindTag,但不知道这个Tag到底有没有什么价值? 以下为这个tag的代码,文章后有sample下载,希望大家能给出意见。 package net.libo.web.taglib; import java.util.ArrayList; import java.util.HashMap; import java.util.Collection; import java.util.Iterator; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.beanutils.PropertyUtils; import net.libo.web.util.HtmlEncoder;import net.libo.web.util.HtmlUtils; /** * @author Boyce * */public class FieldBindTag extends BodyTagSupport { private Object[] beans = null; private String beanNames = null; /** * Sets bean names with value of the "bean" attribute. * * @param v bean names */ public void setBeans(String v) { beanNames = v; } /** * Gets bean names. * * @return bean names */ public String getBeans() { return beanNames; } public int doStartTag() { String[] b = beanNames.split(","); ArrayList beanList = new ArrayList(); for (int i=0;i<b.length;i++) { String name = b[i].trim(); if (name.length() > 0) { Object bean = pageContext.findAttribute(name); if (bean != null)beanList.add(bean); } } beans = beanList.toArray(); return EVAL_BODY_AGAIN; } /** * Performs smart form population. * * @return SKIP_BODY */ public int doAfterBody() { BodyContent body = getBodyContent(); try { JspWriter out = body.getEnclosingWriter(); String bodytext = body.getString(); bodytext = populateForm(bodytext); out.print(bodytext); } catch (Exception ex) { ex.printStackTrace(); } return SKIP_BODY; } /** * End of tag. * * @return EVAL_PAGE * @throws JspException */ public int doEndTag() throws JspException { beans = null; return EVAL_PAGE; } // ---------------------------------------------------------------- populate private String populateForm(String html) throws Exception { int i = 0, s = 0; StringBuffer result = new StringBuffer(html.length()); String currentSelectName = null; while (true) { // find starting tag i = html.indexOf('<', s); if (i == -1) { result.append(html.substring(s)); break; // input tag not found } result.append(html.substring(s, i)); // tag found, all before tag is stored s = i; // find closing tag i = html.indexOf('>', i); if (i == -1) { result.append(html.substring(s)); break; // closing tag not found } i++; // match tags String tag = html.substring(s, i); //System.out.println(tag); String tagName = HtmlUtils.getTagName(tag); if (tagName.equalsIgnoreCase("input") == true) { String tagType = HtmlUtils.getAttribute(tag, "type"); if (tagType != null) { String name = HtmlUtils.getAttribute(tag, "name"); HashMap params = new HashMap(); params.put("name", name); Object vobj = findValue(name); if (vobj != null) { String value = vobj.toString(); tagType = tagType.toLowerCase(); if (tagType.equals("text")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("hidden")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("image")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("password")) { tag = HtmlUtils.addAttribute(tag, "value", value); } if (tagType.equals("checkbox")) { String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue == null) { tagValue = "true"; } if (tagValue.equals(value)) { tag = HtmlUtils.addAttribute(tag, "checked"); } } if (tagType.equals("radio")) { String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue != null) { if (tagValue.equals(value)) { tag = HtmlUtils.addAttribute(tag, "checked"); } } } } } } else if (tagName.equalsIgnoreCase("textarea") == true) { String name = HtmlUtils.getAttribute(tag, "name"); Object value = findValue(name); if (value != null) { if (value != null) { tag += HtmlEncoder.encode(value.toString()); } } } else if (tagName.equalsIgnoreCase("select") == true) { currentSelectName = HtmlUtils.getAttribute(tag, "name"); } else if (tagName.equalsIgnoreCase("/select") == true) { currentSelectName = null; } else if (tagName.equalsIgnoreCase("option") == true) { if (currentSelectName != null) { String tagValue = HtmlUtils.getAttribute(tag, "value"); if (tagValue != null) { Object vals = findValue(currentSelectName); if (vals != null) { if (vals.getClass().isArray()) { Object[] arr = (Object[])vals; if (arr.length > 0) { String[] vs = new String[arr.length]; for (int j=0;j<arr.length;j++) { vs[j] = arr[j].toString(); } for (int k = 0; k < vs.length; k++) { String vsk = vs[k]; if (vsk != null) { if (vsk.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected"); } } } } } else if (vals instanceof Collection) { Collection arr = (Collection)vals; int arrsize = arr.size(); if (arrsize > 0) { String[] vs = new String[arrsize]; int j=0; for (Iterator it=arr.iterator();it.hasNext();) { vs[j++] = it.next().toString(); } for (int k = 0; k < vs.length; k++) { String vsk = vs[k]; if (vsk != null) { if (vsk.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected"); } } } } } else { String value = vals.toString(); if (value.equals(tagValue)) { tag = HtmlUtils.addAttribute(tag, "selected"); } } } } } } result.append(tag); s = i; } return result.toString(); } protected Object findValue(String name) { Object value = null; for (int i=0;i<beans.length;i++) { if (beans[i] != null) { try { value = PropertyUtils.getProperty(beans[i], name); } catch (Exception e) { } if (value != null) break; } } return value; }} |
|
|
|