
Fine, except I only wanted that class to show up for input fields with errors associated with them. That requires logic, and the only way I could figure out how to do this with stock struts was either through lots of nasty logic tags surrounding my div's or through
Dustin Engelhaupt, a fellow I work with entered the picture at this point, and we both sat down to work on this same problem. My first hunch was that we could save ourselves lots of trouble If we could just extend the struts tag libraries to inclued an html:div tag. It turns out happily, that that is exactly what we did. Here is how. Download the struts source. Look in struts-1.2.7-src/src/share/org/apache/struts/taglib/html in there is all the Java Code you will need to do a custom div tag. We initially started by extending the BaseHandlerTag class. However, Dustin pointed out to me that we were going to need access to the property element of the struts tag, becuase that is what the errors are acutally associated with. So an error aware div tag really needs to extend BaseInputTag. We did that like this:[code lang="java"]
/*
* DivTag.java
* Dustin Engelhaupt and josh cronemeyer
*/
package org.j.jsp.tag;
import javax.servlet.jsp.JspException;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.BaseInputTag;
/**
*
* A Class that extends BaseInputTag
* So we can borrow the property functionality
*/
public class DivTag extends BaseInputTag {
private static final long serialVersionUID = 1L;
/**
* Render the beginning of this select tag.
*
* Support for indexed property since Struts 1.1
*
* @exception JspException
* if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
StringBuffer results = new StringBuffer();
results.append("
TagUtils.getInstance().write(pageContext, results.toString());
return (EVAL_BODY_INCLUDE);
}
/**
* Render the end of this form.
*
* @exception JspException
* if a JSP exception has occurred
*/
public int doEndTag() throws JspException {
StringBuffer results = new StringBuffer();
results.append("
1 comment:
Oops. I forgot a line of code in the DivTag class. in the doStartTag() method I need to add results.append(">"); right after the line results.append(prepareStyles()); otherwise the tag won't always close off correctly.
Post a Comment