views:

86

answers:

2

I have problem with JSP tags - how I can insert into attribute value in Java code? This is my code:

<%!
    String ii = new String();
 %>

   <%

    try {
        String id = request.getParameter("a");
        int i = Integer.valueOf(id);
        ii = String.valueOf(i);
    } catch (Exception e) {
        response.sendError(500);
    }

    %>
            <div style="float:right;margin-right:20px;">
                <strong>From: </strong> <em><post:ShowPm postId="<%=ii%>" /></em><br>
            </div>

If I run it, occurs error 500 - stack On line 48 is this code:

<strong>From: </strong> <em><post:ShowPm postId="<%=ii%>" /></em><br>

I am using Struts 1 and JSP with own tags ( is my tag).

+6  A: 

Sounds like you need to change your tld so that attribute can accept expressions:

   <attribute>
      ...
      <rtexprvalue>true</rtexprvalue>
      ...
   </attribute>
danb
ahhh u were faster :D
n00b32
Nice, thaks a lot!!
wokena
+1  A: 

Just going to give some feedback on this JSP in general.

You realise that the variable "ii" won't be threadsafe? ii is a class variable. Only one instance of a servlet is created, and multiple threads run through it. Therefore you have the potential for multiple threads editing this value and printing out inconsistent results.

Solution: declare it as a local variable to the service method in <% scriptlet %> tags rather than <%! declaration %>

Just wondering what is the point of this code? Turn a parameter into a number and then back again? Is this some attempt at validation? To me it seems like a whole lot of confusing unnecessary work. Particularly just turning it back into a String. If it is required to be an integer, why doesn't your tag take an integer? Or do the validation for itself?

evnafets