Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Using custom JSTL tag to write Spring form tags?

  1. #1

    Default Using custom JSTL tag to write Spring form tags?

    Not sure if this is even possible, but wanted to see if anyone had any ideas. If I define a custom tag, like this,

    Code:
    <foo:drawField attr1="foo" attr2="bar"></foo:drawField>
    can the output of that custom tag be a form bind tag, like <form:input/> or something similar that is then processed by the container to render the final HTML in the JSP?

    Basically, I need to draw fields of different types that have different presentations all over an application, but I don't want to duplicate the logic of drawing the field all over the place.

    My thought was to put that into a tag, and then just write out the <form:x> tags that then bind the input to a backing object property.

    Thoughts?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    AFAIK this is not possible, tags are processed at runtime and the result is expected to be HTML not other HTML code.

    However you still could use your tags but instead of using the <form> tags use the old spring:bind tags.

    Code:
    <spring:bind path="command.someProperty">
        <foo:drawField attr1="foo" attr2="bar"></foo:drawField>
    </spring:bind>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Quote Originally Posted by DFielder View Post
    Basically, I need to draw fields of different types that have different presentations all over an application, but I don't want to duplicate the logic of drawing the field all over the place.
    Isn't that what <form:*/> does for you anyway? What would you be doing in your tag that was different?

  4. #4
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    I admit I'm making this suggestion without looking at the code for the Spring form tags, but depending on the modifiers applied to various things you'd need to get at, you *might* be able to create your own custom tag which subclasses the Spring custom tags.

    Isn't that what <form:*/> does for you anyway? What would you be doing in your tag that was different?
    Sure, the Spring form tags certainly don't support every attribute that the HTML equivalents support. I'm not surprised at all that the poster wants slightly different behavior.

    To the OP, you could also think about using DOM manipulation using Javascript after the page is rendered to make whatever modifications you're looking for in the form elements. Depending on what you're changing, this may or may not be feasible.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Sure, the Spring form tags certainly don't support every attribute that the HTML equivalents support. I'm not surprised at all that the poster wants slightly different behavior.
    Hmm I found the list of supported attributes very comprehensive haven't found one (yet) that isn't supported.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    This is true, the majority are supported, however some of the more obscure ones are not (for e.g. "readonly" on textarea, "id" on option, etc). It'll be interesting if the poster responds to indicate what he/she found lacking.

  7. #7

    Default

    I'm not trying to add in HTML attributes that aren't supported - what I'm doing is trying to render a domain object called 'Field' differently based upon the value of a type attribute.

    If the field is of type 'date', then it needs to be rendered with a javascript date control for selecting the date.

    If it's a number field, then it needs to have the ability to select a operator.

    Basically, I don't want to repeat that logic everywhere I have to draw a field in the application.

    What I'm doing now is using c:import and passing the required parameters to the imported JSP, where it draws the field using the logic and the <form: tags.

  8. #8
    Join Date
    Feb 2005
    Location
    Warwickshire, UK
    Posts
    148

    Default

    So, is your custom tag not basically just going to be a big choose/when/otherwise statement? Why can't all the logic for rendering the form input element be contained entirely within the tag?

    I guess I'm still not at all clear what you are trying to do. I don't know if it is any use, but don't forget you can pass chunks of JSP as attributes to a tag, if you want to, say, surround something that is always the same with something that is dependent on context.

    Also, are you trying to do this in a pure java custom tag, or a JSP 2.0 tag file?
    Dave Hewitt
    ------------------
    Senior Systems Engineer
    OBJECTIVITY
    www.objectivity.co.uk

  9. #9

    Default

    Maybe I'm confused on the terminology. What I had intended to do was have a tag like <foo:drawField that takes a bunch of attributes describing the field. Then, the tag implementation, in Java, would write the field HTML to the JspWriter based on business rules.

    I wanted to still take advantage of the Spring <form: tags to bind the field to the backing object, but just writing <form: tags to the JspWriter in my tag didn't work because they weren't being processed - the final HTML just had the actual <form: tag in it.

    I've ended up just doing <c:import, since the imported content is parsed and the <form: tags were appropriately handled to generate the bound HTML form controls.

  10. #10
    Join Date
    Feb 2005
    Location
    Warwickshire, UK
    Posts
    148

    Default

    Right, I understand. Yes, writing direct to the JspWriter like that isn't going to work.

    As of JSP 2.0 you can implement a custom tag in a .tag file. This is a far more expressive way of doing things like this (the difference between tag classes and tag files is analogous to the difference between servlets and JSPs). Google will throw up plenty of examples, but this is the sort of thing I mean:

    Code:
    <%@tag body-content="empty"%>
    <%@attribute name="labelText" required="false" type="java.lang.String"%>
    <%@attribute name="bindPath" required="true" type="java.lang.String"%>
    
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    <spring:bind path="${bindPath}">
          <label for="${status.expression}">${labelText}</label>
          <input type="text"
                 id="${status.expression}"
                 name="${status.expression}"
                 value="${status.displayValue}"/>
    </spring:bind>
    This writes out a simple text input field using the traditional spring:bind tags, rather than the newer html:form tags, but the principal is the same.

    JSP 2.0 is pretty mainstream now, so I'd be surprised if you weren't on it - what container are you using?

    Even if you're stuck with your current approach, I'd think that <jsp:include.../> was preferable to <c:import.../> ?
    Dave Hewitt
    ------------------
    Senior Systems Engineer
    OBJECTIVITY
    www.objectivity.co.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •