GVR
GVR

Reputation: 330

Passing dynamic content to jsp:param "value" attribute

Hie... I was wondering whether it is possible to send dynamic content involving "if" or "switch" functions to jsp:param "value" attribute ... one can send a single value which can be represented in the following manner

<jsp:param name="blah" value="<%=blah%>"/>

now what i mean to ask is ..

<jsp:param name="blah" value="<% 
                               if(blah == 1)
                               out.print("The value is 1");
                               if(blah == 2)
                               out.print("The value is 2");
                               %>"/>

is the above method possible.. when i do the same i get an error stating that an " = " sign is expected after the tag in value attribute ..

Upvotes: 3

Views: 13372

Answers (2)

spiritwalker
spiritwalker

Reputation: 2257

would you consider to change it to

<% if(blah == 1){ %>
<jsp:param name="blah" value="The value is 1"/>
<%}else{%>
<jsp:param name="blah" value="The value is 2"/>
<%}%>

else use equivalent JSTL tags like

<c:choose>
   <c:when test="${blan eq 1}">
      <jsp:param name="blah" value="The value is 1"/>
   </c:when>
   <c:otherwise>
      <jsp:param name="blah" value="The value is 2"/>
   </c:otherwise>
</c:choose>

@gabbi, You do not necessarily have to have two <jsp:forward>, instead you can declare another variable for holding the value, like below:

<% 
String blahValue = ""; 
if(blah == 1){
   blahValue = "The value is 1";
}else if(blah==2){
   blahValue = "The value is 2";
}else{
   blahValue = "the value is invalid"; }
%>

<jsp:forward>
   <jsp:param name="blah" value="<%=blahValue%>"/>
</jsp:forward>

Upvotes: 2

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

I would recommend determining the value of blah prior to executing your jsp. This can be done within a servlet using straight Java. Once you have determined the value of blah place it in the request before forwarding to the jsp.

request.setAttribute("blah", "some value");

Then within your .jsp file you can reference the attribute using jsp expression language.

${blah}

Its best to keep as much logic out of your view (jsp) as possible.

Upvotes: 3

Related Questions