Discussions

Web tier: servlets, JSP, Web frameworks: Using a script variable as an attribute for a JSP tag

  1. I have a variable defined within a JSP:

    <%
    String cust_id = request.getParameter("cust_id");
    %>

    I want to use this variable as a parameter to a custom tag:

    <%@ taglib uri="/tlds/DBTags.tld" prefix="db" %>
    <db:MultiColumnSelect columns="c.id, c.po" name="ccar_id" table_name="ccar_headers c, ccar_rmas r" selectString="r.ccar_id = c.id AND c.cust_id = <%= cust_id %>"/>

    The selectString value that is passed to my tag is:

    r.ccar_id = c.id AND c.cust_id = <%= cust_id %>

    The <%= cust_id %> is not replaced by the actual variable value.

    How would I accomplish using this variable within my JSP tag?
  2. You can't mix and match scripting and fixed text inside custom tag attributes. The solution is to assemble your value in a different variable, and pass *that* to the attribute:

    <%
    String cust_id = request.getParameter("cust_id");
    String selectString = "r.ccar_id = c.id AND c.cust_id = " + cust_id;
    %>

    <db:MultiColumnSelect columns="c.id, c.po" name="ccar_id" table_name="ccar_headers c, ccar_rmas r" selectString="<%= selectString %>"/>
  3. Thanks for your help. I tried the folowing:

    <%
       String select = "cust_id = " + cust_id;
    %>

    <db:MultiColumnSelect name="ccar_id" columns="id, po" separator=" -P.O.# " table_name="ccar_headers" selectString="<%=select%>"/>

    And I still got the error. It appears that they way the tags are processed, they will not allow this type of variable usage. In the above example, the selectString that was passed to my tag was : <%=select%> not the contents of select....

    Oh well, I went ahead and switched it over to a servlet. Works fine now. But all told, that really sucks. The usefulness of tags to me without this capability is 1/3 of what it could be....
  4. The tag developer can explicitly exclude run-time expression values like <%=...%> in tag attributes. This is probably what happened in your case.

    This kind of database logic is better off in a servler or a helper Java class anyway.
  5. I'm the tag developer....

    My purpose for this tag was to provide a means for html developers who have some SQL knowedge to incorporate some read-only information into their webpages without having to learn the whole J2EE thing...

    My Boss here is quite capable in the html arena, but forget trying to get him to write scriplets or servlets or anything of the kind.
  6. Oh! If you are the tag developer, then simply go into the tag library descriptor and change the attribute configuration:

    <attribute>
        <name>[attribute-name]</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>

    By setting rtexprvalue to true, you should be able to use runtime expression values, that is <%=...%> scriptlets, to assign tag attribute values.
  7. Many Thanks...[ Go to top ]

    Thanks a ton for the help. I'm learning JSP, Servlet and Tag development through through a book titled "Java for the Web with Servlets, JSP and EJB. Although it's a great book, sometimes the information gets detached, i.e creating custom tags and the .tld descriptor.

    Thanks again.