Search code examples
javajspwebspherestruts

Embed/nest custom JSP tag to set the attribute value of another HTML tag


I am new to Java and struts and I am working on a project where I need to set the value of an attribute from a custom tag which retrieve the value of a Java property.

...
<cust:urlGeneration porlet="<cust:write property="tgtPortlet"/>">
  <a href="<% wsp.write(out) %>"/>the link</a>
<cust:urlGeneration/>
...

This property is set in a Java class depending on the context. This code is in my corresponding java class :

if(isMyFirstUseCase)
  screenbean.setTgtPortlet = "portlet.myFirstValue";
else
  screenbean.setTgtPortlet = "portlet.mySecondValue";

But it does not work, the portlet attribute is not set correctly (the tag string is not interpreted).

I want the porlet property to be set either with portlet.myFirstValue or portlet.mySecondValue but I do not manage to dynamically set it...

Do I need to escape something or is it simply not possible ? Otherwise anyone has suggestion or alternative solution ?

I can provide any additional information if needed.

Thanks


Solution

  • If it can help anyone, I found a workaround using JSLT :

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    

    I set an intermediate variable with the value of my java class property using my "write" custom tag (Business requirement) :

    <c:set var="varTgtValue"><cust:write property="tgtValue"/></c:set>
    

    Then simply use this new variable to set my JSP tag's property using ${myVar} :

    <cust:urlGeneration porlet="${varTgtValue}">
      <a href="<% wsp.write(out) %>"/>the link</a>
    <cust:urlGeneration/>