Dynamic attributes in custom tag

Hi,
I am new to JSP, and i want to build a custom tag. in this custom tag i have declared for example 5 attributes. i want to have a custom tag that when i set the attribute1 content to "true", only attribute2 and attribute3 would be accessible and if i set the attribute1 content to "false", only attribute4 and attribute5 would be accessible
<%@taglib uri="/WEB-INF/MyTLD2.tld" prefix="myTag" %>
<myTag:DB attribute1 ="true" attribute2="Table1" attribute3="ID,Name,Tel" />
<myTag:DB attribute1 ="false" attribute4="Table1" attribute5=" UpdateTable" />

Or you could go for a more general definition of what the attributes mean. Instead of having 5 attributes that the user only needs 3 of, you could do something like:
<myTag:DB sourceType="string" table="Table1" data="ID,Name,Tel"/>
<myTag:DB sourceType="table" table="Table1" data="UpdateTable"/>
(use just three attributes, all required, but with different expected values for the second or third attribute based on what the first one is).
Then have your tag do:
        if ("string".equals(sourceType)) runStringSourceQuery(table, data);
        if ("table".equals(sourceType)) runTableSourceQuery(table, data);Either way, good documentation is needed to make sure what is needed under what circumstances.

Similar Messages

  • Spec oversight? Using dynamic-attributes in XML tag files

    I have a couple of tagfiles that generate form elements with specific conventions for the 'name' and 'value' attribute. Basically, a data binding framework.
    Dynamic attributes are useful because you can delegate all the usual html attributes to the generated HTML form element: SELECT, INPUT, id, class etc.
    The usual way to do this is to concatenate all the "pass-through" attributes to a string, and append this string to the element generated:
    <input type="hidden" name="foo" ${dynattrs}/>I'm wondering how to do this in a tagfile in XML format (mytag.tagx), since the XML format forbids syntax like in the example above.
    jsp:attribute won't help much, if it's nested inside a c:forEach: it won't apply to the right element.

    Ran into the same problem with 'optional attributes'. (see post "JSP 2.0 Tag files outputting elements with conditional attributes" http://forum.java.sun.com/thread.jspa?forumID=45&threadID=681033)
    Found a solution that is not very elegant but does work and saves you the trouble of reverting to Java Tags. Consider the following tag-file that outputs an html input-tag with conditional attributes:
    <?xml version="1.0" encoding="utf-8"?>
    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
         xmlns:c="http://java.sun.com/jsp/jstl/core">
         <jsp:directive.attribute name="name" required="true" type="java.lang.String"/>
         <jsp:directive.attribute name="id" required="false" type="java.lang.String"/>
         <jsp:directive.attribute name="value" required="false" type="java.lang.String"/>
         <jsp:directive.attribute name="disabled" required="false" type="java.lang.Boolean"/>
         <jsp:directive.attribute name="hint" required="false" type="java.lang.String"/>
         <jsp:directive.attribute name="cssClass" required="false" type="java.lang.String"/>
         <jsp:text><![CDATA[<input type="text" name="]]><c:out value="${name}"/><![CDATA["]]></jsp:text>
         <c:if test="${!empty id}"><![CDATA[ id="]]><c:out value="${id}"/><![CDATA["]]></c:if>
         <c:if test="${!empty cssClass}"><![CDATA[ class="]]><c:out value="${disabled?(cssClass + '-disabled'):cssClass}"/><![CDATA["]]></c:if>
         <c:if test="${disabled}"><![CDATA[ disabled="disabled"]]></c:if>
         <c:choose>
              <c:when test="${!empty value}"><![CDATA[ value="]]><c:out value="${value}"/><![CDATA["]]></c:when>
              <c:when test="${!empty hint}"><![CDATA[ value="]]><c:out value="${hint}"/><![CDATA[" onfocus="if(this.value==']]><c:out value="${hint}"/><![CDATA[')this.value='';"]]></c:when>
         </c:choose>
         <jsp:text><![CDATA[/>]]></jsp:text>
    </jsp:root>In answer to your question: Yes, it looks like an oversight. Using the jsp:attribute tag in a c:forEach doesn't work because the attribute is then applied to the forEach tag. You would have to put the attribute-tag inside a jsp:body tag (inside the forEach). Then it would apply not to the forEach tag but to the tag enclosing the forEach. However, this doesn't work either, or at least it doesn't work in Tomcat 5.5. Could be a bug though, JSP 2.0 is still very buggy (for instance, using a tagfile inside another tagfile from the same taglib doesn't seem to work either...)
    Anyway, if you ever find a good solution please let me know by posting to this topic!
    TIA

  • Is it possible to pass dynamic values to custom tag?

    Hi there, I'm trying to build a calendar custom tag so I can drop the calendar into an existing webpage easily.
    I have got the calendar displaying on the page but the problem I have is when I try to create 'previous' and 'next' links. Is it possible to pass parameters to a custom tag that have dynamic values?
    In PHP it would looks something like:
    <a href="bla.php?page=$pagenumber">next page</a>When I create the calendar object I set it to the current date so when I try to increase or decrease the month (next/prev month) it doesn't work because the code is run again and hence setting the current date again.
    Any ideas?
    Cheers in advance
    Message was edited by:
    MajorMahem

    for eg
    <a href=" Display.jsp?id='+<%=customerId%>">Result Page</a>
    Please try this,
    i didn't work out, any how apply this sample to your code

  • Accepting traditional %= ... % dynamic attribute values in tags

    I am using Tomcat 5.5 (Servlet 2.4). This JSP code works:
    <% String strFoo = "index.jsp"; %>
    <jsp:include page="<%= strFoo %>"/>
    ... but this code that uses a custom tag I wrote doesn't work:
    <% String strFoo = "index.jsp"; %>
    <myutil:check url="<%= strFoo %>">
    some content
    </myutil:check>
    I get an error:
    An error occurred at line: nnn in the jsp file: /xxxxxx.jsp
    Generated servlet error:
    strFoo cannot be resolved
    I have set the "url" attribute's rtexprvalue setting to "true" for the myutil:check tag in its .tld file.
    Does anyone have an idea about what might be wrong? I would have thought that what works for a jsp:include attribute should work for any tag attribute value with runtime evaulation enabled.
    Of course, I would rather be using EL expressions, but I have a large body of existing code that I need to get working with this servlet container quickly, and rewriting it all is not an option at the moment.
    Thanks

    Looks good to me. I can't think of any reason why it shouldn't work.
    Can you please show the tld (or an example tld) defining this tag?
    If you could have some 'dummy' code to duplicate the issue, it will be easier to help.
    One thing you might try - take a look at the code generated by the conversion into servlet. It should be in the [TOMCAT]/work directory.
    See if you can figure out from there why strFoo would not be in scope. Maybe you had it nested in another block?
    Hope this helps,
    evnafets

  • Dynamic include inside custom tag body

    I am trying to perform the dynamic include of another JSP from within the body of a custom tag using JSP1.1. It cannot be done using <jsp:include> since flush=true is required and thus an exception is thrown when called from inside the body of a custom tag - it can be done in JSP1.2 since flush does not have to be set to true but upgrading is not possible at this time.
    Does anyone know of any custom tag or the code needed to perform this function with JSP1.1?
    I am using Tomcat 3.2.3.
    Using a RequestDispatcher and calling include() does not have the desired effect since the output is witten directly to the response and does not go into the correct position in the calling JSP.
    I think the solution involves making a call to the included JSP (is it possible to use a RequestDispatcher that doesn't write directly to the page output stream?) and then append the response to the BodyContent object which is buffering the body content of the custom tag until the doAfterBody() method is called. Is it possible to create a dummy ServletResponse object to pass to the RequestDispatcher and then obtain the HTML response from the internal buffer?

    I don't know if there is a solution to the problem you are having with JSP 1.1. If you look in the spec, it actually warns you that dynamic includes can not be done inside a body tag. The spec actually states that the included code shout write directly to the response and not to the BodyContent.
    I was excited to find out they had fixed this in JSP 1.2, but they seem to have foiled me again. If my tag calls PageContext.include(), the spec says that include must call the flush method, which causes BodyContent to throw an exception since flush is not a valid call.

  • JSF dynamic include and custom tag

    Anybody has experience to enclude custom tags between <f:verbatim> </f:verbatim> ? The custom code embeded this tags won't work, my code as below:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%-- jsf:pagecode language="java" location="/JavaSource/pagecode/jsp/Server_request.java" --%><%-- /jsf:pagecode --%>
    <%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    <%@taglib uri="/WEB-INF/mytaglib.tld" prefix="first" %>
    <HTML>
    <HEAD>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
    <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <META name="GENERATOR" content="IBM Software Development Platform">
    <META http-equiv="Content-Style-Type" content="text/css">
    <LINK href="../theme/Master.css" rel="stylesheet"
         type="text/css">
    <TITLE>server_request.jsp</TITLE>
    <LINK rel="stylesheet" type="text/css" href="../theme/stylesheet.css"
         title="Style">
    </HEAD>
    <f:view>
         <BODY><hx:scriptCollector id="scriptCollector1">
         <P><hx:outputLinkEx styleClass="outputLinkEx" value="header"
                   id="linkEx1">
                   <h:outputText id="text1" styleClass="outputText" value="Header"></h:outputText>
              </hx:outputLinkEx><BR></P>
    <jsp:include page="server_req.jsp"/>
              <hx:outputLinkEx styleClass="outputLinkEx" value="Footer" id="linkEx2">
                   <h:outputText id="text2" styleClass="outputText" value="Footer"></h:outputText>
              </hx:outputLinkEx>
         </hx:scriptCollector></BODY>
    </f:view>
    </HTML>
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
    <%@taglib uri="/WEB-INF/mytaglib.tld" prefix="first"%>
    <f:subview id="server_req">
    <f:verbatim>
    <first:hello/>
    </f:verbatim>
    </f:subview>

    I don't know if there is a solution to the problem you are having with JSP 1.1. If you look in the spec, it actually warns you that dynamic includes can not be done inside a body tag. The spec actually states that the included code shout write directly to the response and not to the BodyContent.
    I was excited to find out they had fixed this in JSP 1.2, but they seem to have foiled me again. If my tag calls PageContext.include(), the spec says that include must call the flush method, which causes BodyContent to throw an exception since flush is not a valid call.

  • Dynamic Attribute Class in Tag Lib TEI?

    Hi All
    I have had pretty good luck implementing a few tag libs.. my only question is..
    Can you dynamically assign the class type of an attribute in the TEI of a Tag? Is there a way to get at the TEI of a tag at runtime?
    As an example of what i'm asking to do, i would like to be able to return the variable "item" in a proper type so as to not have to cast it from a java.lang.object in the jsp script. (see the below code)
    public class myTEI extends TagExtraInfo {
    public VariableInfo[] getVariableInfo(TagData data) {
    return new VariableInfo[] {
    new VariableInfo("index", "java.lang.Integer", true, VariableInfo.NESTED),
    new VariableInfo("item", "java.lang.Object", true, VariableInfo.NESTED)
    Thanks for the help,
    Rick

    I propose you to pass object type as your tag attribute:
    <your:tag ... type="your.type.YourType" />
    And TEI class looks like:
    new VariableInfo("item", data.getAttributeString("type"), true, VariableInfo.NESTED)

  • Custom tag attribute named operation

              Hi,
              I ported a web application from WL6.1sp4 to WL7.0sp2 and expirienced a wired problem.
              All my JSP's did not compile anymore. I received a error message "no setter for
              attribute operation defined", although there was a a method setOperation in the
              current code and there were no code modifications at all.
              After replacing all attributes named operation with opmode the system worked fine
              again.
              Is there any restriction in naming your attributes in custom tags?
              Thanks for help
              Michael
              

              Me again,
              I checked again the parameters of the VM (Sun JDK 1.3.1_06) and tried the the
              different options of the HotSpot compiler. I found out that the problem I reported
              with the last posting can easily be reproduced if you run the VM in HotSpot-Classic
              mode.
              I suppose it is a bug in Sun's VM for Windows. Does somebody have similar expiriences?
              Michael
              "Michael" <[email protected]> wrote:
              >
              >Hi,
              >
              >I ported a web application from WL6.1sp4 to WL7.0sp2 and expirienced
              >a wired problem.
              >All my JSP's did not compile anymore. I received a error message "no
              >setter for
              >attribute operation defined", although there was a a method setOperation
              >in the
              >current code and there were no code modifications at all.
              >
              >After replacing all attributes named operation with opmode the system
              >worked fine
              >again.
              >
              >Is there any restriction in naming your attributes in custom tags?
              >
              >Thanks for help
              >Michael
              

  • Assigning bean property to custom tag's attribute

    1. The #{beanName.property} notation can only be used inside JSF tags.
    2. The attributes of custom tags does not accept embedded tags.
    e.g. <prefix:tagName attrib1="<jsftag>">
    I need to assign a bean's property to the attrib1 attribute. how do I do this?

    Alternatively, how can I assign the value of a bean property to a variable?
    e.g.
    <%
    int var=<bean.property>
    %>
    I can use the variable on the custom tag like this:
    <prefix:tagName attrib1="<%=var%>"> OR
    But this may not be the best solution though because I have to use scriptlets...

  • Questions in Custom Tag

    Hi,
    I have couple of questions in Custom Libraray Tags:
    1) How I can from Tag Java file to open a new JSP window without the toolbars and determine the window size.
    2) How I can read a JSP parameter from Tag Java file. For example I have a field called "x" in JSP is not connected to the TAG by setAttribute.
    3) How I can pass a dynamic value to custom tag. <route:routeList agentID = "1" >
    how I can change the "1" to a field. When I change it to: <route:routeList agentID = "<%=request.getParameter("agentID")%>" > or to <route:routeList agentID = <%=request.getParameter("agentID")%> > I get empty value, even though the field has a value.
    Thank you ...

    [email protected] ... Well, if you put something as an attribute in the pageContext object, you can get it... same as request or session scope attributes, but I thought the OP meant to do this (I guess not, though.. see below):
    <%
    String str = "...";
    %>
    <mytag:stuff .... />
    And within the stuff tag, use str as a variable. I don't believe that's possible, because the compiled page in effect becomes like this:
    String str = "...";
    doStuffTag(whatever the parameters are);
    Obviously that's not the right naming for the tag, but you get the idea...
    JavaUserProg....
    2) First, it's best to put quotes around the tag attributes...
    <INPUT readOnly style="WIDTH: 84px; HEIGHT: 22px" name="operation[<%= lineNum%>"] value="<%=operation%>">
    Second, do you mean you want to read from the tag what the HTML input field has in it? First, you have to submit the form, then the call request.getParameter("fieldname") to get the value. Otherwise, I'm not clear on what you are really trying to do.
    3) If the field in the tag is an int field: setAgentID(int)
    Then you can define the tag value as:
    <mytag:stuff agentID="1" />
    or
    <% int aid = 1; %>
    <mytag:stuff agentID="<%= aid %>" />
    If you just put a static string value like the first way, it converts it. If you put an expression, the expression has to match the type of value it really is. So int for int, String for String, Collection for Collection.

  • Defaulting dynamic attributes and weightings for BID INVITATION

    Hi Friends
    I have a task at hand to default the dynamic attributes( some custom defined fields for the bidder) for bid invitation.
    Can anyone help me with this?
    Thanks in Advance
    Regards

    Hi Ankur,
    I assume you are looking for attributes automatically populated, when a user
    creates a bid invitation. Did you try to work with a template and define the
    bid invitation in the way required and create a bid invitation based on the template?
    Another option is to define the attributes for well defined categories and let the
    users use the "propose" button.
    Thanks,
    Claudia

  • Dynamic attribute in a custom component

    has anybody tried generating an attribute dynamically in a custom component
    e.g
    <cx:inputText value="" foo="footest"/>
    where foo is not defined in the custom inputtexttag.java
    can we have a hashmap which stores all the unknown attributes into the hashmap upon loading and then handle it in the setproperties

    can we have a hashmap which stores all the unknown
    attributes into the hashmap upon loading and then
    handle it in the setpropertiesJSF's UIComponent has a function of generic attributes.
    Use <f:attribute>.
    <cx:inputText value="" foo="footest"/> Anyway, if you want to use the syntax like this, you should provide a custom
    tag handler which has a setFoo() method and whose setProperties() method
    copys the value to the corresponding (generic) attribute of the component.

  • Dynamic include in a custom tag, called from a jsp

    Hello, I would like to be able to dynamically include other jsp's from within a custom tag that I create. is this possible?
    in a jsp page i would just write <%@ include file="file.jsp" %> and all would be ok... but if i do an out.println( "<%@ include file=\"file.jsp\" %>" ); inside a custom tag bean then it doesn't work. it just prints out the command to the page as plain text.
    I do understand why this is happening, but can anyone offer me a way around this problem?
    Thanks in advance,
    Randy

    That's actually the same result as you get with a normal include - The method I suggested is a dynamic include similar to the <jsp:include> tag, not the <%@ include %> tag.
    When you use a dynamic include, the included page is compiled and 'invoked' as serarately, and the result is what gets included, not the actual source. To get the behaviour you want would require an include directive (the <%@ include %> tag) which I don't think has an equivalence you can use inside a custom tag.
    What you can do is pass attributes from the tag class like this:
    pageContext.setAttribute(<name>, <object>, pageContext.REQUEST_SCOPE);
    and then remove them after the include using pageContext.removeAttribute.
    I'm not certain, but this implies that if you declare your variables with a <jsp:usebean> tag with request scope, instead of normal Java variable declarations, you should be able to use them in the page included by the custom tag.
    *** in the jsp ***
    <jsp:useBean id="myVar" scope="request" class="java.lang.String" />
    <% myVar = "Something"; %>
    // Now call the tag which uses the pageContext.include() mehtod.
    *** end ***
    *** in the include ***
    <jsp:useBean id="myVar" scope="request" class="java.lang.String" />
    <%= myVar + " something else" %>
    *** end ***
    Let me know if it works.

  • Custom Tag not evaluating expression in attribute

    I have a custom tag that needs to take dynamic values in the attributes, but I can't seem to get the values "interpreted" correctly. I have the <rtexprvalue> tag set to "true" in my .tld file, which I thought was the only thing that was needed in order to accomplish what I am trying to do. However, that does not seem to be the case.
    I am using WebLogic (8.1.4) and their <netui> tags, along with JSTL tags (1.0).
    An example of what my code looks like is the following:
    <test:myTag id="1" idx="<netui:content value='{container.index}' />">
        <netui:select ... />
    </test:myTag>and
    <c:set var="myIdx" value="<netui:content value='{container.index}' />" />
    <test:myTag id="1" idx="<c:out value='${myIdx}' />">
        <netui:select ... />
    </test:myTag>Neither of the above approaches has worked. In my code for my Tag.java file, I get the literal string values of <netui:content value='{container.index}' /> and <c:out value='${myIdx}' />, respectively, in my idx property.
    Can someone give me any hints as to what I may be doing wrong?
    Thanks.

    Shouldnt that be
    <netui:content value='${container.index}' />Actually, weblogic does not use the '$' prefix before
    their expressions. Fine. Which in turn means weblogic has some custom expression evaluator.
    Note weblogic 8.1
    as a container doesnt implicitly supportexpressions
    and you have to build in that feature into yourtag
    library.Are you referring to the 'isELIgnored' attribute when
    you mentioned the above statement? If not, can you
    explain what you meant by "build that feature into
    your tag library"?
    It's like this - expression language is supported by default in all containers that implement the j2ee 1.4 spec (servlet 2.4/jsp 2.0). Additionally you should also declare your web application to adhere to the 2.4 standards (through the schema definition in web.xml). In applications that refer to the 2.3 dtd but are run on a 2.4 compliant container you can set the 'isELIgnored' attribute to false and use EL. This works because your container anyways supports it.
    If your container doesnt provide support for EL (outside the jstl tags) as is the case with weblogic 8.1, then you can still use expressions by using something like the [url http://jakarta.apache.org/commons/el/]apache common evaluator  package. The difference being that you will have to call the evaluator classes to evaluate the attribute.
    Are there any alternatives that I could use to
    accomplish what I am trying to do?Did the above answer your question?
    ram.

  • Custom tag attribute calculated by scriptlet expression

    Hi,
              If I set the rtexprvalue subelement of the attribute element in my tld to
              "true", should I be able to dynamically determine the value of my custom tag
              attribute using a scriptlet expression?
              When I include the custom tag reference:
              <prod:getCategory id="category"
              categoryID="<%=request.getParameter("catID")%>" scope="page"/>
              it actually gets written to the html as:
              <prod:getCategory id="category" categoryID="2133" scope="page"/>
              and is not recognized as a jsp tag.
              I am using weblogic 6.0 sp1.
              Thanks in advance!
              daniel
              

    I had the same problem in a design a couple of months ago and could not get around it so I switched the functionality into code and out of the tag lib.
    I take it you are trying to do something like this?
    <%
    String value="hiworld";
    %>
    <mytaglib:saysomething value="<%=value%>"/>

Maybe you are looking for