Announcement Announcement Module
No announcement yet.
spring:input instead of spring:bind, input, and error check Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring:input instead of spring:bind, input, and error check


    I'm sorry if I've chosen a wrong topic, or if there has already been some discussion about something similiar in the last days. Anyways, I want to give it a try:

    In some of my projects, I'm quite often using something like this:

    <spring&#58;bind path="person.address">
      <input name="<c&#58;out value="$&#123;status.expression&#125;"/>" type="text" value="<c&#58;out value="$&#123;status.value&#125;"/>" style="width&#58;100%;"
        <c&#58;when test='$&#123;status.errorMessage != ""&#125;'>
          class="error" />Error&#58; <c&#58;out value="$&#123;status.errorMessage&#125;" />
    The hole part is always the same, whereas only the bind path changes.
    I'd thought about something like this:

    <spring:input path="person.address" />

    Dot. I think this would make lots of JSPs easier to read and develop.
    Of course, this short tag could be extended to some extra parameters, such as e.g. errorClass="error" (regarding the CSS) or noErrorClass="default' and so on.
    Additionally, this is also possible for comboboxes, radiogroups and so on, but I see the biggest benefit on input boxes.

    May be some of you like this idea. Please let me know.

    If anybody could at least give me a hint where to start with doing something like this (inheriting a java class from Spring's default Taglibrary? No idea, sorry) I'd do it myself and provide a patch, as I'm sure this would be very useful, and not only for myself.

    Kind regards,

  • #2
    I have been asking for the same thing but the spring team does not intend on providing such functionality. Actually, they intend on providing form simplification macros for JSP 2.0. Such functionality already exists for velocity and freemarker (my favorite). However, if you are using JSP and your container supports only JSP 1.2 as in Weblogic 8.1, then you will always have to use the verbose bind tag. I like the bind tag for its flexibility but things can get pretty verbose when you start outputting select boxes, radio buttons etc ... Therefore, I thought about implementing JSP 1.2 custom tags with EL support to mirror spring's freemarker macros ( Unfortunately, I have not had a chance to do so. You can go ahead and start implementing it. After looking at the following classes, you'll find that it is pretty easy to do.

    org.springframework.web.servlet.tags.HtmlEscapingA wareTag
    org.springframework.web.servlet.tags.BindTag ;
    org.springframework.web.util.ExpressionEvaluationU tils;

    I would say the key classes are BindStatus, ExpressionEvaluationUtils and HtmlEscapingAwareTag. Perhaps, you can create a base class for the Html form tags by extending HtmlEscapingAwareTag. A subclass like HtmlFormInputTag can use BindStatus and ExpressionEvaluationUtils to do the magic and then render everything in the JspWriter.

    I think many spring users would benefit from such tags. I know it goes against what Rod preached in his book i.e. tags should not render html, but, I believe sometimes compromises have to be made for the sake of simplicity.


    • #3
      Why don't you just create a JSP page.


      <jsp:include page="MyInput.jsp">
      <jsparam name="path" value="command.person.address"/>

      and then inside MyInput.jsp ....

      <spring:bind path="${param.path}">
      ... etc.

      This has saved us a *huge* amount of development and debugging time, by creating Checkbox.jsp, Text.jsp, TextArea.jsp, MultiSelect.jsp (two list boxes side-by-side), Select.jsp, CheckboxList.jsp, Date.jsp, Grid.jsp, etc, etc, etc ...


      • #4
        Not a bad idea. Two years ago, I worked on a project where something similar was implemented with struts tiles tags. Can you share the basic templates i.e. radio, checkbox, select etc ...?


        • #5
          Sounds like a poor man's tag lib to me.


          • #6
            I guess we might convert the JSPs into tags at some stage, but while they're sort of changing a bit as we get more Spring experience it's easier to leave them as JSPs.

            Our web designers are also sort of semi proficient in JSP scriptlets, etc, and so it makes life easier for them since they can tweak the JSPs and leave us to do the rest of the app.

            Would we get a speedup from tag caching if we converted to taglibs?, or do modern web containers provide caching for JSP pages that are included multiple times anyway?


            • #7
              I just did a simple taglib for input type=text. Code posted on my blog.