Java

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Using custom tags and struts together

    7 answers - 1310 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hello all
    Has anybody successfully used Stuts tags and Custom tags together?
    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] + "</html:link>");
    The custom tag appears to write the output directly to the socket without further Struts
    tag evaluation, consequently the HTML source on the browser includes "html:link" which
    it doesn't understand.
    1. How about writing the link using <a href=/WEB-INF/pages/update.jsp></a>
    When I do this I lose the connection to my current session. Not a solution.
    2. Try return true in the "theBodyShouldBeEvaluatedAgain" method.
    I tried this and the code got into a loop (I believe this simply recurses back through my
    code). If I am supposed to use this somebody will need to tell me a simple way of
    stopping the recursion.
    3. The only other alternative I have is to look into the Struts source code and try and
    find some internal method to call to evaluate the generated code before I print it to the
    socket.
    Hope somebody has tried to do this before
    Kind regards
    mc
    FCUS Computing
    Mob: 0415 24 26 24
    murray (AT) focus-computing (DOT) com
    http://www.focus-computing.com
  • No.1 | | 807 bytes | |

    From: "Murray Collingwood" <murray (AT) focus-computing (DOT) com>

    Has anybody successfully used Stuts tags and Custom tags together?

    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] +
    "</html:link>");

    I haven't done anything with custom tags in a while, but isn't this the same
    as writing (in a JSP):
    <my:tag attr="<other:tag action="abc"/>"/>
    ? Which, as you've noted, won't work.

    What does your custom tag do? The 'entry[ix]' looks like it might be
    iterating. If so can you do it with <c:forEachand <html:linkalone?
    Alternately, do you really need the <html:linkor can you just write an <a
    href=""directly?
  • No.2 | | 1703 bytes | |

    Normally it doesn process html:link. Did you put the tld for html:link
    on the page? Does your tag extend BodyTagSupport? I think I remember
    having to execute the print in doEndTag() as well.

    Murray Collingwood wrote:
    Hello all

    Has anybody successfully used Stuts tags and Custom tags together?

    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] + "</html:link>");

    The custom tag appears to write the output directly to the socket without further Struts
    tag evaluation, consequently the HTML source on the browser includes "html:link" which
    it doesn't understand.

    1. How about writing the link using <a href=/WEB-INF/pages/update.jsp></a>
    When I do this I lose the connection to my current session. Not a solution.

    2. Try return true in the "theBodyShouldBeEvaluatedAgain" method.
    I tried this and the code got into a loop (I believe this simply recurses back through my
    code). If I am supposed to use this somebody will need to tell me a simple way of
    stopping the recursion.

    3. The only other alternative I have is to look into the Struts source code and try and
    find some internal method to call to evaluate the generated code before I print it to the
    socket.

    Hope somebody has tried to do this before

    Kind regards
    mc

    FCUS Computing
    Mob: 0415 24 26 24
    murray (AT) focus-computing (DOT) com
    http://www.focus-computing.com

    To unsubscribe, e-mail: user-unsubscribe (AT) struts (DOT) apache.org
    For additional commands, e-mail: user-help (AT) struts (DOT) apache.org
  • No.3 | | 2854 bytes | |

    Murray Collingwood wrote:
    Hello all

    Has anybody successfully used Stuts tags and Custom tags together?

    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] + "</html:link>");
    >

    The custom tag appears to write the output directly to the socket without further Struts
    tag evaluation, consequently the HTML source on the browser includes "html:link" which
    it doesn't understand.

    This says 'write the text "<html:link " to the HTTP response', as you
    say. It seems that what you are trying to do is create a custom tag which
    outputs JSP source, which you then want processed as if it had appeared
    literally in the JSP page. In other words, you want to write '<x:mytag/>'
    in the JSP and have the rendered result be as if you'd actually written
    '<html:link '. Is that right?

    1. How about writing the link using <a href=/WEB-INF/pages/update.jsp></a>
    When I do this I lose the connection to my current session. Not a solution.

    What do you mean by 'loose the connection to my current session?' Do you
    mean that when the link is clicked the resulting request isn't associated
    with the session? If cookies are enabled it should be, but if cookies are
    turned off you need to include the session ID in the URL the same as Struts
    and JSTL tags do.

    2. Try return true in the "theBodyShouldBeEvaluatedAgain" method.
    I tried this and the code got into a loop (I believe this simply recurses back through my
    code). If I am supposed to use this somebody will need to tell me a simple way of
    stopping the recursion.

    Evaluate Body is used when you want the body of the tag to be evaluated,
    not when you want the /output/ of the tag to be evaluated. Think, for
    example, of forEach and the like.

    3. The only other alternative I have is to look into the Struts source code and try and
    find some internal method to call to evaluate the generated code before I print it to the
    socket.

    I think it would help to take a step back and describe the overall problem
    you're trying to solve. If I understand you correctly, you're trying to
    write a custom tag that generates JSP source code and have that generated
    JSP code evaluated inline as part of the same processing cycle that calls
    your custom tag. I suspect there are easier ways to do what you need

    If all you want to do is include a link in the HTML (not JSP) markup
    generated by your tag and ensure that link includes JSESSINID when needed,
    try looking at the code for the <html:link/tag to see how the session ID
    and URL rewriting is handled.

    HTH,

    L.
  • No.4 | | 2395 bytes | |

    8/18/05, Murray Collingwood <murray (AT) focus-computing (DOT) comwrote:
    Hello all

    Has anybody successfully used Stuts tags and Custom tags together?

    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] + "</html:link>");

    Your question doesn't really describe what you are illustrating
    which appears to be that you want to dynamically create a *JSP* page,
    then have the page compiled and executed, on every request. You're
    going to find that this solution is not practical, and that you should
    take another approach.

    When you first start a webapp, you notice the slight delay when you
    access a JSP page the first time, so that it gets compiled? Even if
    you jumped through all the technical hoops to output a JSP file and
    get it compiled (technically feasible), you're going to be imposing
    that kind of overhead on *every* request, instead of just the first
    request for that page. Even if everything else in your app was
    instantaneous, the response time of your app would be awful.

    A far better approach for the particular case you are using here is to
    use either an expression or some subordinate tag to calculate the
    dynamic part of your output. Assume for a moment that your Struts
    action puts the "entry" array into request scope. Now you can do
    something like:

    <html:link action="Update.do">
    <c:out value="${entry[3]}"/>
    </html:link>

    course, the reason you probably want to do this in the first place
    is that you want to create a bunch of links so put the same sort
    of structure inside a looping tag (renaming the request scope
    attribute containing your array to be "entries" to be more readable):

    <c:forEach var="entry" items="entries">
    <html:link action="Update.do">
    <c:out value="$entry"/>
    </html:link>
    </c:forEach>

    The <c:forEachtag exposes the current element in the array as a page
    scope attribute named by the "var" attribute, and you can therefore
    reference the current element directly inside the loop.

    Craig

    To unsubscribe, e-mail: user-unsubscribe (AT) struts (DOT) apache.org
    For additional commands, e-mail: user-help (AT) struts (DOT) apache.org
  • No.5 | | 4522 bytes | |

    Thanks for the responses to my question.

    I agree totally with what you are saying, to try generating Struts tags from a Custom tag
    is completely the wrong approach - I can see that now (you know, "I see the light" :-)

    Let me explain what I am trying to do, maybe somebody can point me in the right
    direction.

    My website provides a login capability and each user has a security setting, this is just a
    number (security isn't too complicated). Number 0 can simply browse around and make
    comments etc. Security number 5 is allowed to manage the content, add and update
    database records that contain data that appear on the site. Number 3 is allowed similar
    content management access but only on certain records.

    When I display a page that contains some database records I then want to display
    "Update" and "Delete" links next to the appropriate data.

    I could use a <logic:greaterThantag to test the value of the security number and
    then display the link if appropriate. This gets more complicated when the security
    number 3 is used, as then I have to test values on the record as well, suddenly I have
    nested <logic:tags and the page is starting to look a lot more complicated than it
    should. The other problem with this approach is: "whether a user can update a record
    or not" is a business logic decision and shouldn't be in the interface.

    Now, to complicate it even further, security number 2 is allowed to update records but
    uses a different form, in other words, they are only allowed to update certain values on
    the record. Hence I have to vary the link generated in some instances.

    I hope you are getting the picture. This is why I was trying to use a custom tag that
    could still interact with my model, call business methods to make security decisions and
    vary the generated link accordingly.

    And finally the question: How should I go about writing the "Update" link now that we all
    understand the problem?

    Thanks in advance.

    Kind regards
    mc

    19 Aug 2005 at 15:30, Craig McClanahan wrote:

    8/18/05, Murray Collingwood <murray (AT) focus-computing (DOT) comwrote:
    Hello all

    Has anybody successfully used Stuts tags and Custom tags together?

    For example:
    Consider a custom tag with the following code:
    out.println("<html:link action=\"Update.do\">" + entry[ix] + "</html:link>");

    Your question doesn't really describe what you are illustrating
    which appears to be that you want to dynamically create a *JSP* page,
    then have the page compiled and executed, on every request. You're
    going to find that this solution is not practical, and that you should
    take another approach.

    When you first start a webapp, you notice the slight delay when you
    access a JSP page the first time, so that it gets compiled? Even if
    you jumped through all the technical hoops to output a JSP file and
    get it compiled (technically feasible), you're going to be imposing
    that kind of overhead on *every* request, instead of just the first
    request for that page. Even if everything else in your app was
    instantaneous, the response time of your app would be awful.

    A far better approach for the particular case you are using here is to
    use either an expression or some subordinate tag to calculate the
    dynamic part of your output. Assume for a moment that your Struts
    action puts the "entry" array into request scope. Now you can do
    something like:

    <html:link action="Update.do">
    <c:out value="${entry[3]}"/>
    </html:link>

    course, the reason you probably want to do this in the first place
    is that you want to create a bunch of links so put the same sort
    of structure inside a looping tag (renaming the request scope
    attribute containing your array to be "entries" to be more readable):

    <c:forEach var="entry" items="entries">
    <html:link action="Update.do">
    <c:out value="$entry"/>
    </html:link>
    </c:forEach>

    The <c:forEachtag exposes the current element in the array as a page
    scope attribute named by the "var" attribute, and you can therefore
    reference the current element directly inside the loop.

    Craig

    To unsubscribe, e-mail: user-unsubscribe (AT) struts (DOT) apache.org
    For additional commands, e-mail: user-help (AT) struts (DOT) apache.org
  • No.6 | | 1810 bytes | |

    Murray Collingwood wrote:

    >I could use a <logic:greaterThantag to test the value of the security number and
    >then display the link if appropriate. This gets more complicated when the security
    >number 3 is used, as then I have to test values on the record as well, suddenly I have
    >nested <logic:tags and the page is starting to look a lot more complicated than it
    >should. The other problem with this approach is: "whether a user can update a record
    >or not" is a business logic decision and shouldn't be in the interface.
    >
    >Now, to complicate it even further, security number 2 is allowed to update records but
    >uses a different form, in other words, they are only allowed to update certain values on
    >the record. Hence I have to vary the link generated in some instances.


    If the pages are radically different then perhaps you could use
    completely different presentations based on a user's access
    level than passing a different result set to the page,
    generating different links, etc. you could just send them to an entirely
    different JSP.

    I've found that once my page logic goes beyond a simple "show this chunk
    or don't" it's been easier (not always cleaner, I suppose) to have
    completely separate pages altogether. That said, I've only rarely had to
    go that far: usually passing a different collection in to the JSP,
    setting a true/false to choose between two links, etc. has been enough.

    I'm interested in how other folks have dealt with similar issues.

    Dave

    To unsubscribe, e-mail: user-unsubscribe (AT) struts (DOT) apache.org
    For additional commands, e-mail: user-help (AT) struts (DOT) apache.org
  • No.7 | | 1414 bytes | |

    I hope you are getting the picture. This is why I was trying to use a
    custom tag that could still interact with my model, call business methods
    to make security
    decisions and vary the generated link accordingly.

    And finally the question: How should I go about writing the "Update" link
    now that we all understand the problem?

    You could put all that logic somewhere else, perhaps in a bean with an
    'isUpdateAllowed' method:
    <c:if test="${security.updateAllowed}<html:link </c:if>

    Since you're already okay with a custom tag, what about extending the Struts
    link tag to do what you need?

    I also wonder if you really need <html:linkfor this-- you're already
    hard-coding the action name. If there's nothing dynamic about the link
    other than that 'entry[ix]' part, then can you just write out the <a
    href=""from your custom tag?

    I've had success with request.isUserInRole-- *without* getting into custom
    Realms and CMA-- just add a Filter, wrap the request and override
    'isUserInRole'. It sounds like you have some "levels" that could be roles.
    (I'm not clear on the runtime checking of fields you mentioned, but you
    should have access to the request and session from isUserInRole.) Struts
    has <logic:present role=""(and there's probably a JSTL equivalent).

Re: Using custom tags and struts together


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "Java"

EMSDN.COM