Extending Spring form tags
- #Java
- #Spring Boot
- #HTML
- #Tips
- 2018/08/24
Extending form tags
- Every form tag checks its binding (via the
pathattribute). If the model has no data bound to that path, you get a binding error. - Most classes call
resolveId, which automatically generates theidattribute tied to the path when rendering HTML. - If you do not want an
id(e.g., to avoid JS hooks), overrideresolveIdand returnnullwhen no explicit value is provided.
Building conditional tags
Override writeTagContent inside your tag class and return whether to evaluate the body. That is how you implement conditional logic.
Outputting raw text without TagWriter
Not recommended, but if you must render raw text, skip TagWriter. Spring tags inherit PageContext, so you can write directly via JspWriter:
this.pageContext.getOut().print("text")
Other tips
- You can grab the request from
pageContext. - After creating a custom tag, declare it in a TLD with the class’s fully qualified name (place the TLD under
WEB-INF). - Add a
taglibdirective in any JSP that uses the custom tag.
Share:
X (Twitter)